Not only is new MERGE command able to do UPSERTS.It can also do DELETE.-- Prepare sample data DECLARE @Base TABLE (ID INT, Data INT) INSERT @Base SELECT 1, 1 UNION ALL SELECT 2, 1 DECLARE @New TABLE (ID INT, Data INT) INSERT @New SELECT 1, 0 UNION ALL SELECT 2, 1 UNION ALL SELECT 2, 1 UNION ALL SELECT 3, 1 -- BeforeSELECT *FROM @Base-- Do the magicMERGE @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 DELETEWHEN MATCHED THEN UPDATE SET b.data = src.DataWHEN NOT MATCHED THEN INSERT (ID, Data) VALUES (src.ID, src.Data); -- Last statement must end with a comma-- AfterSELECT *FROM @Base
E 12°55'05.25"N 56°04'39.16"