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:

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/1699281.html

(0)
上一篇 2025年2月19日 14:17:28
下一篇 2025年2月19日 14:17:43

AD推荐 黄金广告位招租... 更多推荐

相关推荐

发表回复

登录后才能评论