Introduction
This detailed article will discuss the Merge statement in SQL Server.
We are going to cover,
- What is MERGE Statement
- Syntax of MERGE Statement
- UPDATE Record using MERGE Statement.
- INSERT Record using MERGE Statement.
- DELETE Record using MERGE Statement.
- TOP Clause with MERGE Statement.
- OUTPUT clause with MERGE Statement.
What is MERGE Statement?
In the real world, we often need to sync or merge two tables. For example, we have two tables
- Target
- Source
We want to sync both tables.
I mean,
- if the record does not exist in the target table, we want to insert it from Source Table; if a record exists in the Target Table, then update it from the Source table.
- Or the record exists in the source table and not in the target table, then deleted from the Target table.
Any guess as to how will you achieve it before SQL 2008?
The answer would be writing three different SQL statements based on the condition: Insert, Update and Delete. This is where the MERGE statement comes into the picture. It will help you to achieve this in a single SQL statement instead of three different statements.
"The Merge statement is a very popular clause in SQL which is mainly used to handle three different statements (insert, Update and Delete) in a single statement called MERGE."
With MERGE Statement, we require two tables,
- Source Table – This table contains the changes that must be applied to the Target Table.
- Target Table – The table which required changes.
Let's see the below image to get an understanding,

Syntax of MARGE Statement
MERGE TOP (value) <target_table>
USING <table_source>
ON <merge_search_condition>
[ WHEN MATCHED [ AND <clause_search_condition> ]
THEN <merge_matched> ]
[ WHEN NOT MATCHED [ BY TARGET ] [ AND <clause_search_condition> ]
THEN <merge_not_matched> ]
[ WHEN NOT MATCHED BY SOURCE [ AND <clause_search_condition> ]
THEN <merge_matched> ]
[ <output_clause> ]
[ OPTION ( <query_hint> ) ]
;
Please Note that MERGE Statement must terminate by ";"
To get a better understanding, we are going to create two tables like below,

The below script helps you to create tables with sample data.
CREATE TABLE [dbo].[MemberSource](
[Id] [int] NOT NULL,
[Name] [varchar](30) NOT NULL
)
GO
CREATE TABLE [dbo].[MemberTarget](
[Id] [int] NOT NULL,
[Name] [varchar](30) NOT NULL
)
GO
INSERT INTO MemberSource
values(1,'Kirtesh'),
(2,'Nitya')
GO
INSERT INTO MemberTarget
values(1,'Rajesh'),
(3,'Mahesh')
GO
Now we will use the MERGE Statement to Insert, Update and Delete records in the "MemberTarget" table from the "MemberSource" table.
UPDATE MATCH RECORDS WITH MERGE
Both of the tables have ID=1. If data exists in the target table, then update the record from the source table. So, in this case, we will update the description from MemberSource to the MemberTarget table.
WHEN MATCH Clause – This Clause is used to UPDATE and DELETE records based on match conditions.
Only Two WHEN MATCHED clauses can be in the MERGE Statement. If we have 2 WHEN MATCHED clauses in the MERGE Statement,
- One should be used for an UPDATE
- And another should be used for DELETE.
Both WHEN MATCHED Clause cannot be used with MERGE's UPDATE OR DELETE statement.
In our case, "MemberSource" is the source table, "MemberTarget" is the Target table, and ID is used for joining conditions.
Let's see the below script,
MERGE MemberTarget T
USING MemberSource S ON T.ID=S.ID
WHEN MATCHED THEN
UPDATE SET T.Name= S.Name;










Join the conversation! Your thoughts help the community grow.