update A
set A1=B.B1,A2=B.B2 from A,B
where A.AID=B.BID
上述MSSQL语句在ORACLE中该怎么写?

解决方案 »

  1.   

    update A  set a1=(select b1 from b where a.aid=b.bid),
                  a2=(select b2 from b where a.aid=b.bid)
    where exists (select 1 from b where a.aid=b.bid)
      

  2.   

    update A set (A1,A2) = (select B.B1,B.B2 from B where A.AID=B.BID)
    where exists (select 1 from B where A.AID=B.BID);
      

  3.   

    谁能解释下where 后面条件的意思?
      

  4.   

    where是确保只更新A.AID=B.BID的记录,不满足条件的不更新。
    如果没有where条件会全部更新,A.AID<>B.BID的记录会更新为null
      

  5.   

    exists (select 1 from B where A.AID=B.BID)若存在A.AID=B.BID的记录,exists子句返回true,否则返回false;因为只去验证存在不存在记录,因此无所谓select子句中的列,此处仅1代列exists 以短路方式(short_circuit)执行,即A,B表中如果存在多条ID相同的记录,oracle在检索到第一条记录时便返回true.
      

  6.   

    还可以这么写
    merge into A 
     using(select B1, B2 from B) b
     on (a1=b.B1 and a2=b.B2)
     when matched then update set AID = b.BID
      

  7.   

    不好意思,看错了。
    应该这么写
    update A 
    set A1=B.B1,A2=B.B2 from A,B 
    where A.AID=B.BID merge into A 
    using(select B1, B2,BID from B) b 
    on (AID = b.BID) 
    when matched then update set a1=b.B1 and a2=b.B2 
      

  8.   


    必须确保=后面的值是unique