Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does sql-server prevent inserting in WHEN MATCHED of merge?

Tags:

sql-server

Anyone know why sql server prevents inserting from within the WHEN MATCHED clause of a MERGE statement? I understand that the documentation only allows updates or deletes, I'm wondering why this is the case so I can understand merge better.

Look at this post for an example.

like image 740
Anthony Elliott Avatar asked Oct 22 '22 06:10

Anthony Elliott


2 Answers

If you are trying to merge your source to your target, it does not make sense to insert a line if it was found in the target. You may want to update or delete it though. Inserting what is already there would create duplicates.

like image 162
ApplePie Avatar answered Oct 25 '22 17:10

ApplePie


As you want to INSERT when you find a MATCH, i presume the condition of the ON-clause is met but another field is different. Consider including this field into the ON-clause with AND to differentiate between present rows and to be inserted rows.

like image 20
Der U Avatar answered Oct 25 '22 19:10

Der U