zaagan
7/31/2019 - 7:11 PM

MS SQL Output Clause

MS SQL Output Clause


-- Show Inserted tables after insertion
INSERT TableName (Col1,Col2)
OUTPUT Inserted.*
VALUES( Val2,Val2)

-- Show New And Old Data during update
UPDATE Table1
SET Col1 = 'NewValue'
    OUTPUT deleted.Col1 AS OldData, 
           inserted.Col1 AS NewData
WHERE SomeCol = 'SomeData'

-- With Table Variables, Sample
DECLARE @DeletedData Table (
     SupplierID UNIQUEIDENTIFIER,
     ProductID UNIQUEIDENTIFIER
);

DELETE Supplier 
    OUTPUT deleted.ProductID, deleted.SupplierID
	INTO @DeletedData
	WHERE ContactID = '5';

SELECT *FROM @DeletedData;