简体   繁体   English

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

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

I want to Update or insert Oracle Table based on condition. 我想根据条件更新或插入Oracle表。

Consider that in the table have 2 columns (like id and name), one or more name having the same id. 考虑在表中有2列(如id和name),一个或多个name具有相同的id。 In this situation i want to check the id and name (like 1,'Buyer'), if its exist then i want to update name 'Buyer' to 'Service Provider'. 在这种情况下,我想检查ID和名称(例如1,“买方”),如果它存在,那么我想将名称“买方”更新为“服务提供商”。 otherwise i want to just insert the values (1,'Service Provider'). 否则,我只想插入值(1,“服务提供者”)。

I tried this through Merge, but it's update all the name column of id 1 to 'Service Provider'. 我通过合并尝试了此操作,但是它将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');

Available data in the table: 表格中的可用数据:

1  Buyer
1  Buyer Agent
1  Vendor

Thanks in advance. 提前致谢。

you need to join on both columns 您需要同时加入两列

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