Skip to content
  • There are no suggestions because the search field is empty.

T SQL Script: Delete rows safely inside a transaction

T SQL Script: Delete rows safely inside a transaction

-- Delete rows safely inside a transaction
BEGIN TRANSACTION;

DELETE FROM dbo.Orders
OUTPUT deleted.OrderID, deleted.OrderDate
WHERE OrderDate < '2020-01-01';

SELECT @@ROWCOUNT AS rows_deleted;

-- Change to COMMIT only after checking the rows and count.
ROLLBACK TRANSACTION;