oracle update關聯表的思路總結

2021-12-30 11:05:21 字數 876 閱讀 6717

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  www.2cto.com  

(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: 單行子查詢返回多個行

www.2cto.com  

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:

思路4:merge update更新

oracle update多表關聯

update a.a3 a.a3 b.b3 的問題 表a 結構 a1 a2 a3 表b 結構 b1,b2,b3 其中 a1 b1 為pk 切值相同 就是可以使用a1 b1 了.請問用sql 語句或過程該如何實現如下的功能?更新a 表的 a3 用a.a3 與b.b3之和更新.3 update a se...

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 ex...

oracle UPDATE 多表關聯更新

update customers a set city name select b.city name from tmp cust city b where b.customer id a.customer id where exists select 1 from tmp cust city b ...