I have a T-SQL
script that implements some synchronization logic using OUTPUT
clause in MERGE
s and INSERT
s.
Now I am adding a logging layer over it and I would like to add a second OUTPUT
clause to write the values into a report table.
I can add a second OUTPUT
clause to my MERGE
statement:
MERGE TABLE_TARGET AS T
USING TABLE_SOURCE AS S
ON (T.Code = S.Code)
WHEN MATCHED AND T.IsDeleted = 0x0
THEN UPDATE SET ....
WHEN NOT MATCHED BY TARGET
THEN INSERT ....
OUTPUT inserted.SqlId, inserted.IncId
INTO @sync_table
OUTPUT $action, inserted.Name, inserted.Code;
And this works, but as long as I try to add the target
INTO @report_table;
I get the following error message before INTO
:
A MERGE statement must be terminated by a semicolon (;)
I found a similar question here, but it didn't help me further, because the fields I am going to insert do not overlap between two tables and I don't want to modify the working sync logic (if possible).
UPDATE:
After the answer by Martin Smith I had another idea and re-wrote my query as following:
INSERT INTO @report_table (action, name, code)
SELECT M.Action, M.Name, M.Code
FROM
(
MERGE TABLE_TARGET AS T
USING TABLE_SOURCE AS S
ON (T.Code = S.Code)
WHEN MATCHED AND T.IsDeleted = 0x0
THEN UPDATE SET ....
WHEN NOT MATCHED BY TARGET
THEN INSERT ....
OUTPUT inserted.SqlId, inserted.IncId
INTO @sync_table
OUTPUT $action as Action, inserted.Name, inserted.Code
) M
Unfortunately this approach did not work either, the following error message is output at runtime:
An OUTPUT INTO clause is not allowed in a nested INSERT, UPDATE, DELETE, or MERGE statement.
So, there is definitely no way to have multiple OUTPUT
clauses in a single DML statement.