My ERP Vendor has the following trigger on a table:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TRIGGER [dbo].[SOItem_DeleteCheck]
ON [dbo].[soitem]
FOR DELETE
AS
BEGIN
DECLARE @RecCnt int, @LogInfo varchar(256)
SET @RecCnt = (SELECT COUNT(*) FROM deleted)
IF @RecCnt > 150
BEGIN
RAISERROR (54010, 18, 1, 'SOItem') WITH LOG
ROLLBACK TRANSACTION
END
SET @LogInfo = 'Deleting ' + LTRIM(STR(@RecCnt)) + ' Rows From SOItem'
EXEC LogDeletes @LogInfo
END
GO
This seems very inefficient to me. Doesn’t select count(*) take longer than Count(specific field)?
Honestly even if is is slower, I can run a select stament like that in less than a millisecond on my largest table that has millions of rows which this trigger is unlikely to hit. There is no real performance gain from changing it. I’m curious as to why you would want to rollback any transaction with more than 150 records thoug.