ITPub博客

首页 > 数据库 > Oracle > oracle中要谨慎使用update交叉更新!

oracle中要谨慎使用update交叉更新!

原创 Oracle 作者:warehouse 时间:2008-12-09 09:45:00 0 删除 编辑
在oracle中使用update进行交叉更新时一直都觉得很不方便,当然这是和sql server or sybase相比较而言的。[@more@]

SQL> select *from a1;

ID N SFZHAO
---------- - ------------------
1 a
1 c
2 a
2 c
3 c 123

SQL> select *from a2;

ID N SFZHAO
---------- - ------------------
1 c 111
2 d 111

--如果使用下面的sql来更新表a1的话,我们发现出现了我们不希望出现的结果,把a1中id=3所对应的sfzhao由原来的123更新成了null,这是我们不希望的。

SQL> update a1 set sfzhao=(select a2.sfzhao from a2 where a2.id=a1.id and a2.nam
e=a1.name);

已更新5行。

SQL> select *from a1;

ID N SFZHAO
---------- - ------------------
1 a
1 c 111
2 a
2 c
3 c

SQL> rollback;

回退已完成。

--为了纠正上面update所产生的错误结果,在where字句中做了进一步的限定。

SQL> update a1 set sfzhao=(select a2.sfzhao from a2 where a2.id=a1.id and a2.nam
e=a1.name) where (id , name) in (select id , name from a2 where a2.id=a1.id and
a2.name=a1.name);

已更新 1 行。

SQL> select *from a1;

ID N SFZHAO
---------- - ------------------
1 a
1 c 111
2 a
2 c
3 c 123

SQL>

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/19602/viewspace-1014388/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: SQL Access Advisor!
请登录后发表评论 登录
全部评论

注册时间:2007-12-07

  • 博文量
    717
  • 访问量
    5366336