日期:2014-05-17  浏览次数:20807 次

怎样实现两张表多个字段的更新
例如A表   存在一下字段:
AID       A1         A2           A3       A4

B表中存在字段:
BID       B1         B2           B3       B4

如果实现用B表的所有字段更新A表中相应的字段,在MS   SQL   Server里面可以写成:
update   A
set   A1=B.B1,A2=B.B2,A3=B.B3,A4=B.B4
from   A,B
where   A.AID=B.BID

但是在oracle   9i里面怎么样,好像向SQL   Server不一样?
请各位大侠帮助!


------解决方案--------------------
update A
set (A1,a2,a3,a4) = (
select b.b1,b.b2,b.b3,b.b4
from B
where A.AID=B.BID)
------解决方案--------------------
update a
set (a1,a2,a3,a4) = (
select b.b1,b.b2,b.b3,b.b4
from b
where a.aid=b.aid)
where exists (select 1 from b where a.aid=b.bid)

加上EXISTS会好些 保证ID之间关联只能返回一条记录 否则报错