SQL Server 2008 has introduced a new statement called MERGE which will combine the logic of INSERT-IF NOT EXITS and UPDATE-IF EXISTS.
MERGE statement will give you the ability to write one single statement which will update the row if it already exists otherwise it will insert a new row.
Additionally, MERGE statement also enable you to combine the logic of delete if you want.
Following example from Microsfot SQL Server 2008 Books Online
will give you the better idea:
Use AdventureWorks
Go
MERGE Departments AS d
USING Departments_delta AS dd
ON (d.DeptID = dd.DeptID)
WHEN MATCHED AND d.Manager dd.Manager
OR d.DeptName dd.DeptName
THEN UPDATE SET d.Manager = dd.Manager,
d.DeptName = dd.DeptName
WHEN NOT MATCHED THEN
INSERT (DeptID, DeptName, Manager)
VALUES (dd.DeptID, dd.DeptName, dd.Manager)
WHEN SOURCE NOT MATCHED THEN
DELETE;
[...] Sites i got my info from: http://agilebi.com/cs/blogs/jwelch/archive/2007/07/05/sql-server-2008-using-merge-from-ssis.aspx http://technet.microsoft.com/en-gb/library/bb522522.aspx http://sqltips.wordpress.com/2007/08/27/merge-statement-of-sql-seerver-2008/ [...]
Pingback by Using the Merge statement in SSIS | Kasper de Jonge BI Blog — April 15, 2009 @ 6:35 PM |