MERGE modifies data based on one of the following conditions
- When the source matches the target
- When the source has no match in the target
- When the target has no match in the source
MERGE statement is very handy
improvement for T-SQL developers who have to update database tables with
complicated logic. MERGE statement also improves the performance of database as
it passes through data only once.
Simple Demos
Using Simple Query
Steps
1. Create the Following 2 Tables: [Source] and [Target] in any Database running
the following Query:
CREATE Table [Source] ( id int, name varchar(50))
CREATE
Table [Target]
( id int,
name varchar(50),
status varchar(10))
2. Insert some Dummy data on both tables
TRUNCATE TABLE [Source]
TRUNCATE TABLE [Target]
INSERT INTO [Source]
VALUES (1, 'abc'), (2,'pqr' ), (3, 'xyz')
INSERT INTO [Target](id, name)
VALUES
(1,
'abc'),
(2,'sdfdf'),
(4,
'abc')
3. INSERT INTO a new table tempTarget1 From [dbo].[Target] using following
Query:


Join the conversation! Your thoughts help the community grow.