cancel
Showing results for 
Search instead for 
Did you mean: 

Merge with insert and delete at the same time

Taifunov
Discoverer
0 Kudos
1,199

I have table LOCNAG_ENT and jsut for test i cloned this table and changed some ENTERPRISE_ID for exact LOCACCESSGRP_ID and trying to merge.

But when it goes to DELETE i have "syntax error near 'DELETE' on line 5'
And i can't catch why?
And also do we have some similar behaviour like in SLQ Server? For "WHEN MATCH BY SOURCE"


MERGE INTO LOCNAG_ENT as tgt
USING LOCNAG_ENT_CLONE src
on src.LOCACCESSGRP_ID=tgt.LOCACCESSGRP_ID and src.ENTERPRISE_ID=tgt.ENTERPRISE_ID
when matched then skip
WHEN NOT MATCHED and src.ENTERPRISE_ID <> tgt.ENTERPRISE_ID and src.LOCACCESSGRP_ID <> tgt.LOCACCESSGRP_ID THEN DELETE
WHEN NOT MATCHED and tgt.LOCACCESSGRP_ID <> src.LOCACCESSGRP_ID and tgt.ENTERPRISE_ID <> src.ENTERPRISE_ID THEN INSERT (LOCACCESSGRP_ID, ENTERPRISE_ID) values(src.LOCACCESSGRP_ID, src.ENTERPRISE_ID)


How can i procceed with logic if i have same values from both tables (agrp_id, enterpsie_id) then just skip, if i have new values in source table but doesn't have in target, then insert, if i doesn't have values in source table which have target table, then delete them?

Accepted Solutions (0)

Answers (1)

Answers (1)

chris_keating
Product and Topic Expert
Product and Topic Expert

DELETE is not permitted in a NOT MATCH.

Taifunov
Discoverer
0 Kudos

How then can i procceed with logic if i have same values from both tables (agrp_id, enterpsie_id) then just skip, if i have new values in source table but doesn't have in target, then insert, if i doesn't have values in source table which have target table, then delete them?

chris_keating
Product and Topic Expert
Product and Topic Expert

A second MERGE that deals with the DELETE but formed as a WHEN MATCHED would be an option.