Wednesday, October 17, 2007

SQL Server Forums - More fun with Katmai DUPSERT?

 An insert, delete and update in a single transaction?

-- Do the magic MERGE @Base AS b USING ( SELECT ID, SUM(Data) FROM @New GROUP BY ID ) AS src (ID, Data) ON src.ID = b.ID WHEN MATCHED AND src.Data = 0 THEN DELETE WHEN MATCHED THEN UPDATE SET b.data = src.Data WHEN NOT MATCHED THEN INSERT (ID, Data) VALUES (src.ID, src.Data); -- Last statement must end with a comma

SQL Server Forums - More fun with Katmai

No comments: