If I have multiple WHEN MATCHED statements in a MERGE statement, do they all execute if they\'re true?
My example:
DECLARE @X bit = NULL;
--skipping the
I found in the MSDN documentation:
WHEN MATCHED THEN
Specifies that all rows of target_table that match the rows returned by ON , and satisfy any additional search condition, are either updated or deleted according to the clause.
The MERGE statement can have at most two WHEN MATCHED clauses. If two clauses are specified, then the first clause must be accompanied by an AND clause. For any given row, the second WHEN MATCHED clause is only applied if the first is not. If there are two WHEN MATCHED clauses, then one must specify an UPDATE action and one must specify a DELETE action. If UPDATE is specified in the clause, and more than one row of matches a row in target_table based on , SQL Server returns an error. The MERGE statement cannot update the same row more than once, or update and delete the same row.
So it looks like only one of the statements are executed, and they require a DELETE in one and an UPDATE in the other.