Skip to content
Advertisement

SQL Merge Into with multiple statements

I’m trying to keep a historic table of another one. When updating the original I would like to insert rows into the historic one. I’m using Sql Merge:

Can I make multiple statements in the same “when” condition?, as such:

Advertisement

Answer

Normally you can INSERT only into one table. The syntax does not allow multiple statements in the same “when” condition.

But, SQL Server has OUTPUT clause which allows to add another table. It is very handy when you need to have some sort of auditing trail.

See this question How to INSERT into multiple tables from one SELECT statement

So, add OUTPUT clause to your MERGE statement. Something like this:

This will capture both updates and inserts in anotherTable. To capture only inserts you can output at first into a temp table and then filter results by MERGE $action.

Have a look at this question:

Pipes and filters at DBMS-level: Splitting the MERGE output stream

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