Oracleupdate關聯表的思路總結
來源:懂視網
責編:小采
時間:2020-11-09 09:41:31
Oracleupdate關聯表的思路總結
Oracleupdate關聯表的思路總結: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 -
導讀Oracleupdate關聯表的思路總結: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 -

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:
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com
Oracleupdate關聯表的思路總結
Oracleupdate關聯表的思路總結: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 -