繁体   English   中英

使用合并在单个表中插入和更新?

[英]Insert and Update in Single Table using Merge?

我想根据条件更新或插入Oracle表。

考虑在表中有2列(如id和name),一个或多个name具有相同的id。 在这种情况下,我想检查ID和名称(例如1,“买方”),如果它存在,那么我想将名称“买方”更新为“服务提供商”。 否则,我只想插入值(1,“服务提供者”)。

我通过合并尝试了此操作,但是它将ID 1的所有名称列更新为“服务提供商”。

merge into party_type p
using (select 1 party_id, 'Buyer' party_type from dual) t
  on (t.party_id = p.party_id)
when matched then
  update set party_type = 'Service Provider'
when not matched then
  insert (party_id,party_type) values(1,'Service Provider');

表格中的可用数据:

1  Buyer
1  Buyer Agent
1  Vendor

提前致谢。

您需要同时加入两列

merge into party_type p
using (select 1 party_id, 'Buyer' party_type from dual) t
  on (t.party_id = p.party_id and t.party_type = p.party_type)
when matched then
  update set party_type = 'Service Provider'
when not matched then
  insert (party_id,party_type) values(1,'Service Provider');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM