Merge

 

Merge in SQL Server

It allow us to perform insert update and deletes in one statement.

 

1. Source Table: - Contains the change that’s needs to be applied to the target table.

2. Target Table: - The table that require changes (insert, updates, and deletes)

 

Merge [target_table] as T

Using [Source_table] as S

on T.id=S.id

when matched then

   update set T.name=s.name

when not not matched by target then

   insert (id,name) values(s.id,s.name)

when not matched by source then

    delete