Skip to content

Instantly share code, notes, and snippets.

@ifarbod
Created May 21, 2023 21:53
Show Gist options
  • Save ifarbod/8d7c302d811ea3b900e8352836d92319 to your computer and use it in GitHub Desktop.
Save ifarbod/8d7c302d811ea3b900e8352836d92319 to your computer and use it in GitHub Desktop.
count the number of deleted rows using an after-delete trigger
-- count the number of deleted rows using an after-delete trigger
-- sql server 2019
CREATE TABLE Orders (
OrderID int IDENTITY (1, 1) NOT NULL,
Names nchar (15) NULL,
)
GO CREATE TRIGGER Deleted ON Orders FOR DELETE AS
DECLARE @Count int
SELECT @Count = COUNT(*)
FROM DELETED IF @Count > 0 BEGIN PRINT @Count
END
GO
INSERT INTO Orders
values ('yes')
GO
DELETE FROM Orders
GO
INSERT INTO Orders
values ('yes'),
('yessss'),
('yesss'),
('yesss'),
('yesss')
GO
DELETE FROM Orders
GO DROP TRIGGER Deleted
GO DROP table Orders
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment