Different OUTPUT when using the MERGE statement?

I use MERGEto insert or update records in a table:

MERGE INTO target 
USING SELECT * FROM @source
WHEN MATCHED THEN
UPDATE SET ...columns...
WHEN NOT MATCHED THEN
INSERT ...columns...
OUTPUT inserted.* INTO @insertedRecord

If the above statement performs an update, is the updated record in the table variable updated?

+4
source share
2 answers

$action will give you the type of action.

inserted.* will provide you with new values ​​from updates and values ​​for inserted rows.

deleted.* will give you old values ​​for updates and values ​​for deleted rows.

You might want to paste this into a temporary table

OUTPUT $action, inserted.*, deleted.*, @source.* into #changes

and work with it to get the results you are looking for.

+2

.

inserted psuedotable , , .

deleted , , ( ).

: , . OUTPUT @source.* ( ).

+3

Source: https://habr.com/ru/post/1666743/


All Articles