What is MERGE statement?

 Posted by Rahulshukla on 11/11/2011 | Category: Sql Server Interview questions | Views: 5469 | Points: 40
Answer:

MERGE is new feature in SQL Server 2008 that provide an efficient way to perform multiple operations. In previous version we had to write separate statement to INSERT,DELETE and UPDATE data based on certain conditions, but now using MERGE statement we can include the logic of such data modification in one statement that even checks when the data matched then just update it and when unmatched then insert it. most important advantage of MERGE statement is all the data is read and processed only once.


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Posted by: Vuyiswamb on: 1/5/2012 | Points: 10
Can you please Explain more, i dont understand, can you make an example where you use the Merge in SQL
Posted by: Tmosha on: 1/5/2012 | Points: 10
It's one of the good features of sql server 2008,check the below syntax for merger

MERGE #temp AS t

USING #temp1 AS t1
ON t.ID = t1. ID
WHEN MATCHED THEN
UPDATE SET C.Colname= NC.Colname
WHEN NOT MATCHED THEN
INSERT (ColName, ColName1) VALUES (NC.ColName1,NC.ColName1);

in place of #temp1 you can also have select statement with parameters that you need to insert into #temp.
Based on the matching condition it will update or insert records.
Posted by: Vuyiswamb on: 1/5/2012 | Points: 10
Thanks

Login to post response