首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > oracle >

Oracle多表联接更新

2012-10-21 
Oracle多表连接更新两个表A,B通过字段ID INNER JOIN连接,现想更新表B中的QTY值为表A的QTY值,但只更新这两

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; 

热点排行
Bad Request.