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.
Instead of writing a subquery in the WHERE clause, you can use the MERGE statement to join rows from a source tables and a target table, and then delete from the target the rows that match the join condition.
The OUTPUT clause was introduced in SQL Server 2005 version. The OUTPUT clause returns the values of each row that was affected by an INSERT, UPDATE or DELETE statements. It even supports with a MERGE statement, which was introduced in SQL Server 2008 version.
Merging tables by columns. Multiple tables can be merged by columns in SQL using joins. Joins merge two tables based on the specified columns (generally, the primary key of one table and a foreign key of the other).
At most, we can specify only two WHEN MATCHED clauses in the MERGE statement. If two WHEN MATCHED clauses are specified, one clause must have an update operation and the other one must use delete operation.
Not possible. See the grammar.
The Merge statement has
[ <output_clause> ]
The square brackets show it can have an optional output clause. The grammar for that is
<output_clause>::= { [ OUTPUT <dml_select_list> INTO { @table_variable | output_table } [ (column_list) ] ] [ OUTPUT <dml_select_list> ] }
This clause can have both an OUTPUT INTO
and an OUTPUT
but not two of the same.
If multiple were allowed the grammar would have [ ,...n ]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With