When using multiple WHEN MATCHED statements, do they all execute, or does only one get executed?

前端 未结 3 1320
感情败类
感情败类 2021-02-07 02:32

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         


        
3条回答
  •  日久生厌
    2021-02-07 03:21

    well, the answer is, do you really want to because if you do you would change a set based update to a row by agonising slow row update as in a set of rows you really would not know what columns changed on a record by record basis.

    the question, therefore, is do you want to get performance? if so, make sure you have indexes that covering the

    WHEN MATCHED TARGET.FIELD1 = SOURCE:FIELD1 AND TARGET.FIELD2 = SOURCE:FIELD2 ... 
    

    if not you are going to have to cursor over your updates after the merge using an INSTEAD OF trigger...

    Not good for speed, however, can work if you need to record who did what...

    Happy Coding

    Walter

提交回复
热议问题