【问题标题】:Insert and Update in Single Table using Merge?使用合并在单个表中插入和更新?
【发布时间】:2015-10-20 05:40:38
【问题描述】:

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

假设表中有 2 列(如 id 和 name),一个或多个 name 具有相同的 id。在这种情况下,我想检查 id 和名称(如 1,'Buyer'),如果它存在,那么我想将名称 'Buyer' 更新为 'Service Provider'。否则我只想插入值(1,'服务提供者')。

我通过 Merge 进行了尝试,但它会将 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

提前致谢。

【问题讨论】:

    标签: oracle merge


    【解决方案1】:

    你需要加入两列

    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');
    

    【讨论】:

    • 我试过这个,但我得到了错误,之后只有我在 Stackoverflow 中提出这个问题。
    • 错误是 ON 子句中引用的列无法更新:“P”.“PARTY_TYPE”。有什么简单的方法可以做到这一点。
    猜你喜欢
    • 2011-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-28
    • 2016-01-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多