你好,游客 登录 注册 搜索
背景:
阅读新闻

Oracle update关联表的思路总结

[日期:2012-09-17] 来源:Linux社区  作者:linwaterbin [字体:大 中 小]

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:

思路4:merge update更新 http://www.linuxidc.com/Linux/2012-08/67443.htm

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款