当前位置: 代码迷 >> Oracle管理 >> Oracle多表联接更新
  详细解决方案

Oracle多表联接更新

热度:78   发布时间:2016-04-24 05:02:05.0
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 Busing Aon(B.id = a.id)when matched then   update set B.QTY=A.QTY where B.QTY<>A.QTY;
  相关解决方案