两个表A,B通过字段ID INNER JOIN连接,现想更新表B中的QTY值为表A的QTY值,但只更新这两个QTY不一样的部分,一样的不动。

解决方案 »

  1.   

    晕, 一样的动不动有什么区别吗?方式1, updateupdate a
    set a.qty = (select qty from b where a.id = b.id)
    where exists (select 1 from b where a.id = b.id and a.qty <> b.qty)
      

  2.   

    方式2, merge intomerge into a
    using (select b.* from  a, b where a.id = b.id and a.qty <> b.qty ) b
    on (a.id = b.id)
    when matched then
     update set a.qty = b.qty
    when not matched then
      insert (a.id,a.qty)
      values(b.id,b.qty)
      

  3.   


    merge into B
    using A
    on(B.id = a.id)
    when matched then
       update set B.QTY=A.QTY where B.QTY<>A.QTY;
      

  4.   

    update a 
    set a.qty = (select qty from b where a.id = b.id) 
    where exists (select 1 from b where a.id = b.id and a.qty <> b.qty)
      

  5.   

    都可以。一个是没有找到的情况下就为insert一条建议2楼
      

  6.   

    merge into B
    using A
    on(B.id = a.id)
    when matched then
       update set B.QTY=A.QTY where B.QTY<>A.QTY;