Articles → SQL SERVER → Merge command in SQL Server

Merge command in SQL Server








Source and Target















Syntax


-- Syntax
MERGE  Table1 T
using Table2 S
   ON S.column_name = T.column_name
WHEN NOT MATCHED
THEN INSERT(TargetColumn1, TargetColumn2,...) VALUES(SourceColumn1, SourceColumn2,...)
WHEN MATCHED
THEN UPDATE SET T.Column1 = S.Column1; -- Could be more than one column





Sample















Click to Enlarge



-- Query
	MERGE  OfficeTarget T
using OfficeSource S
   ON S.office_id = T.office_id
WHEN NOT MATCHED
THEN INSERT(office_location) VALUES(office_location)
WHEN MATCHED
THEN UPDATE SET T.office_location = S.office_location;





Click to Enlarge




Click to Enlarge




Click to Enlarge




Click to Enlarge




Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Sunday, January 31, 2016

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250