from:http://www.tkk7.com/pts/archive/2010/11/23/338825.html
sqlserver和oracle中實現update關聯更新的語法不同,都可以通過inline view(內嵌視圖)
來實現,總的來說sqlserver更簡單些. 測試例子如下:
create table tmp_a
(cpcode varchar2(10),
sb_ym varchar2(6),
flag char(1)
);
create table tmp_b
(cpcode varchar2(10),
sb_ym varchar2(6),
flag char(1)
);
insert into tmp_a(cpcode,sb_ym,flag)values('3201910001','200406','e');
insert into tmp_a(cpcode,sb_ym,flag)values('3201910002','200406','e');
insert into tmp_b(cpcode,sb_ym,flag)values('3201910001','200406','r');
insert into tmp_b(cpcode,sb_ym,flag)values('3201910002','200406','r');
insert into tmp_b(cpcode,sb_ym,flag)values('3201910003','200406','r');
insert into tmp_b(cpcode,sb_ym,flag)values('3201910004','200406','e');
commit;
在SQLSERVER中:
update tmp_b set flag = b.flang from tmp_a a,tmp_b b
where a.cpcode =b.cpcode and a.sb_ym = b.sb_ym;
在Oracle中:
方法一:(效率低)
update tmp_b a
set flag = (select flag from tmp_a b
where a.cpcode = b.cpcode and a.sb_ym = b.sb_ym )
where exists
(select * from tmp_a c
where a.cpcode = c.cpcode and a.sb_ym = c.sb_ym);
Statistics
----------------------------------------------------------
8 recursive calls
3 db block gets
18 consistent gets
0 physical reads
0 redo size
方法二:(效率高)
alter table tmp_a add constraint p_tmp_a primary key (cpcode, sb_ym);
update (select b.flag flagb,a.flag flaga
from tmp_a a,tmp_b b
where a.cpcode=b.cpcode
and a.sb_ym=b.sb_ym)
set flagb=flaga;
Statistics
----------------------------------------------------------
0 recursive calls
3 db block gets
7 consistent gets
0 physical reads
0 redo size
注意:方法二中數據源表必須要加上主鍵,否則會報錯
ORA-01779: 無法修改與非鍵值保存表對應的列
被修改的表則無需增加主鍵