[英]Oracle Merge: When not matched then delete and insert
我有一個簡單的查詢,但是只有一部分使我感到困惑:
我有以下算法:
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;
您可以通過刪除幫助我,然后插入還是告訴我解決方法?
您不能在WHEN MATCHED THEN UPDATE子句中插入。 您只能在這里刪除。 以及為什么只需要更新就需要刪除然后再插入? 像這樣:
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 ...
從下面的評論中,我想您需要這樣的東西:
1)沒有參考table_1的外鍵
2)在表_1中禁用主鍵val2
3)更新table_1設置val2 = null;
4)運行合並
5)從table_1刪除,其中val2為空;
6)在表_1中啟用主鍵
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.