简体   繁体   English

Oracle合并:不匹配时删除并插入

[英]Oracle Merge: When not matched then delete and insert

I have a simple query but just one part confuses me: 我有一个简单的查询,但是只有一部分使我感到困惑:

I have the following algorithm: 我有以下算法:

merge into table_1 table_2
on table_1.val1 = table_2.val1
when matched and table_1.val2 = table_2.val2
   then merge
when matched and table_1.val2 != table_2.val2
   then delete and insert ( I AM NOT SURE NOW TO DO THIS)
when not matched
   then insert;

Can you hep me with the delete and insert or tell me a way around? 您可以通过删除帮助我,然后插入还是告诉我解决方法?

You cannot insert in the WHEN MATCHED THEN UPDATE clause. 您不能在WHEN MATCHED THEN UPDATE子句中插入。 You can only DELETE here. 您只能在这里删除。 And why do you need to delete and then insert if you can just update? 以及为什么只需要更新就需要删除然后再插入? Something like this: 像这样:

merge into table_1 
using table_2 on (table_1.val1 = table_2.val1)
when matched then 
     update set table_1.val3 = 
                case when table_1.val2 = table_2.val2 
                     then table_1.val3 
                else table_2.val3 end
when not matched then insert ...

From the comments below I suppose you need something like this: 从下面的评论中,我想您需要这样的东西:

1) There is no foreign keys which reference table_1 1)没有参考table_1的外键
2) Disable the primary key val2 in table_1 2)在表_1中禁用主键val2
3) update table_1 set val2 = null; 3)更新table_1设置val2 = null;
4) run merge 4)运行合并
5) delete from table_1 where val2 is null; 5)从table_1删除,其中val2为空;
6) Enable the primary key in table_1 6)在表_1中启用主键

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

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