ORACLE UPDATE 语句误区

 
    当需要用一个表里的记录去更新另一个表是,往往容易陷入update语句语法误区,请注意下面例子:
create table TABLEA
(
  ID   NUMBER,
  NAME VARCHAR2(100)
) ;
prompt Creating TABLEB...
create table TABLEB
(
  ID   NUMBER,
  NAME VARCHAR2(1111)
) ;
insert into TABLEA (ID, NAME) values (1, 'A1');
insert into TABLEA (ID, NAME) values (2, 'A2');
insert into TABLEA (ID, NAME) values (3, 'A3');
insert into TABLEA (ID, NAME) values (4, 'A4');
commit;
insert into TABLEB (ID, NAME) values (1, 'B1');
insert into TABLEB (ID, NAME) values (2, 'B2');
commit;
 
错误写法:
UPDATE TABLEA  SET 
TABLEA.NAME=(select tableb.name from tableb where tablea.id=tableb.id);
select * from tablea 结果为
id       name
1        B1
2        B2
3        NULL
4        NULL
 
正确写法:
UPDATE TABLEA  SET 
TABLEA.NAME=(select tableb.name from tableb where tablea.id=tableb.id)
where exists (select tableb.name from tableb where tablea.id=tableb.id );
 
select * from tablea 结果为
id       name
1        B1
2        B2
3        A3
4        A4

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

转载于:http://blog.itpub.net/297293/viewspace-694855/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值