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

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

------解决方案--------------------
晕, 一样的动不动有什么区别吗?

方式1, update

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)
------解决方案--------------------
方式2, merge into

merge 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)
------解决方案--------------------
SQL code

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