现在有如下表
create table t1 (a1 int, a2 nvarchar2(20));
create table t2 (b1 int, b2 nvarchar2(20));
insert into t1 values (1,'a');
insert into t1 values (2,'b');
insert into t1 values (3,'c');
insert into t1 values (4,'d');
insert into t2 values (1,'A');
insert into t2 values (2,'B');
现在我们需要实现如下操作 "update t1.a2=t2.b2 where t1.a1=t2.b1'
在sqlserver中,update可以支持from 子句,可以写为
update t1 set t1.a2=t2.b2 from t1,t2 where t1.a1=t2.b1;
在Oracle中,update语句不支持from子句,可以写为
update t1 set t1.a2=(select b2 from t2 where t1.a1=t2.b1 ) where exists(select b2 from t2 where t1.a1=t2.b1 );
需要注意的是,在给t1.a2的赋值语句中,如果不加上 where exists 子句的筛选,那么在t1.a2中存在而在t2.b2中不存在的记录将被更新为
null值,这点需要小心。
[@more@]来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/9236282/viewspace-1029603/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/9236282/viewspace-1029603/