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

在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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值