Oracle update關聯表的思路總結

1、 其中最普通的是update t1 set b=(select b from t2 where t1.a=t2.a); 但是,要注意空值的影響, 如果怕空值的影響,要寫成 update t1 set tname= (select sname from t2 where t1.id=t2.id) where exists – (select 1 from t2 where t1.id=t2.id); 2、對視

1、

其中最普通的是update t1 set b=(select b from t2 where t1.a=t2.a);

但是,要注意空值的影響,

如果怕空值的影響,要寫成

update t1 set tname= (select sname from t2 where t1.id=t2.id)

where exists

(select 1 from t2 where t1.id=t2.id);

2、對視圖的UPDATE語句

update (

select /*+use_hash(t1,t2)*/ t1.tname b1,t2.sname b2

from t1,t2 where t1.id=t2.id)

set b1=b2;

這種方法效率高,但是要注意兩個關聯字段都要有唯一性索引!避免ORA-01427: 單行子查詢返回多個行

3、存儲過程

declare

cursor c is select t1.*,t1.rowid from t1;

begin

for c1 in c

loop

update t1 set tname=(select sname from t2 where id=c1.id)

where rowid=c1.rowid and exists (select 1 from t2 where c1.id=t2.id);

end loop;

end;

但是還是要注意要有exists的語句,否則一樣解決不了空值問題

4、merge也可以進行update:

? 版權聲明
THE END
喜歡就支持一下吧
點贊14 分享