Skip to content
Advertisement

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

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:

And this works, but as long as I try to add the target

I get the following error message before INTO:

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:

Unfortunately this approach did not work either, the following error message is output at runtime:

So, there is definitely no way to have multiple OUTPUT clauses in a single DML statement.

Advertisement

Answer

Not possible. See the grammar.

The Merge statement has

The square brackets show it can have an optional output clause. The grammar for that is

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 ]

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement