Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple OUTPUT clauses in MERGE/INSERT/DELETE SQL commands?

Tags:

I have a T-SQL script that implements some synchronization logic using OUTPUT clause in MERGEs and INSERTs.

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.

like image 324
Alexander Galkin Avatar asked Jun 18 '13 09:06

Alexander Galkin


People also ask

Can we use DELETE in MERGE 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.

What is output $action in SQL Server?

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.

Can we use multiple tables in MERGE statement?

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).

Can we use with clause in MERGE statement?

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.


1 Answers

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 ]

like image 171
Martin Smith Avatar answered Dec 06 '22 12:12

Martin Smith