The MERGE Statement in SQL Server 2008

The MERGE Statement in SQL Server 2008

Share on Facebook0Share on Google+0Tweet about this on TwitterShare on LinkedIn0Share on Reddit0
The MERGE Statement is one of the new T-SQL enhancements in SQL Server 2008. So what actually is the MERGE Statement? When and how should it be used?

The MERGE Statement actually allows joining a source with a target table and then based on the results of the join, it performs insert, update, or delete operations on the target table.

A simplified syntax example of the MERGE Statement is the following:

MERGE [tableA] as target using [tableB] as source on target.id = source.id
when matched then update set target.col1 = source.col1, target.col2 = source.col2
when not matched then insert values (source.id, source.col1, source.col2)
when not matched by source then delete;

So let’s analyze the above syntax. I will use some steps for that.

Step 1: Declare your source and target tables.

Step 2: Declare a joining condition on the two tables (in our example is the target.id = source.id condition).

Step 3: Set the action for the when matched case. This case means that a record was found that exists in both tables. To this end, you should perform an update in order to synchronize the two records.

Step 4: Set the action for the when not matched case. This case means that a record was found in the source table which does not exist in the target table. To this end, you should perform an insert in order to copy the record from the source to the target.

Step 5: Set the action for the when not matched by source case. This case means that a record was found in the target table which does not exist in the source table. To this end, you can delete this record (if the purpose of this operation is the synchronization of the two tables).

Please note that the MERGE statement should always end with a semicolon (;).


Recommended eBooks on SQL Server:

Tuning SQL Server: eBook by SQL Server MVP Artemakis Artemiou
Tuning SQL Server: eBook by SQL Server MVP Artemakis Artemiou
Administering SQL Server: eBook by SQL Server MVP Artemakis Artemiou
Administering SQL Server: eBook by SQL Server MVP Artemakis Artemiou
Artemakis Artemiou
Artemakis Artemiou is a Senior SQL Server Architect, Author, Software Developer and a Microsoft Data Platform MVP. He has over 15 years of experience in the IT industry in various roles. Among other, via his initiative SQLEBooks.com, Artemakis authors and publishes eBooks on different topics on SQL Server. Artemakis currently serves as the President of the Cyprus .NET User Group (CDNUG) and the International .NET Association Country Leader for Cyprus (INETA). Additionally he is the founder of the SQLArtBits initiative that aims to provide the technical community with simple, yet powerful and high-quality SQL Server tools. Currently, the highlights of these tools are DBA Security Advisor and In-Memory OLTP Simulator. Artemakis's official website can be found at aartemiou.com. Artemakis's blogs can be found at: SQLNetHub.com and TechHowTos.com.

One thought on “The MERGE Statement in SQL Server 2008

  1. Artemakis Artemiou [MVP]

    Something I would also like to mention regarding the MERGE statement is that by adding in the statement the following code: OUTPUT $action, Inserted.*, Deleted.*; every time the MERGE statement is executed you will get information on which records were updated, inserted and deleted.

    So based on this post's example the code can be modified as follows in order to include the OUTPUT clause:

    —————————
    MERGE [tableA] as target using [tableB] as source on target.id = source.id
    when matched then update set target.col1 = source.col1, target.col2 = source.col2
    when not matched then insert values (source.id, source.col1, source.col2)
    when not matched by source then delete
    OUTPUT $action, Inserted.*, Deleted.*;
    —————————