Please rate this item: 1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...

MERGE Transact-SQL statement example

I have only just found out about the “MERGE” transact-SQL statement that was introduced in SQL Server 2008.

From https://docs.microsoft.com/en-us/sql/t-sql/statements/merge-transact-sql:

Performs insert, update, or delete operations on a target table based on the results of a join with a source table. For example, you can synchronize two tables by inserting, updating, or deleting rows in one table based on differences found in the other table.

To investigate this further, I have created a “MergeExample” database on my local instance, and created a Beverages table, and an Updates table to hold the new version of Beverages 1:

Now here is the Merge statement in action:

Now check the Beverages table after the changes:

  1. Please ensure you test any script taken from my website on a test/development machine, before running on a production server.
Updated: Thursday, 29th March, 2018 — 11:22 am

Leave a Reply

Your email address will not be published. Required fields are marked *

CJayMead's SQL Server, Endeca, MS Office Help © 2018