Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.
General best practices for SQL Server triggers
From Wiki
In general the first thing you should check in a trigger is if there is anything to do, you can do this by using @@ROWCOUNT. If @@ROWCOUNT returns 0 then you should exit the trigger. Below is a little code example, I took the trigger definition from Books On Line and added the IF @@ROWCOUNT = 0 RETURN part
- CREATE TRIGGER NewPODetail2
- ON Purchasing.PurchaseOrderDetail
- AFTER INSERT AS
- IF @@ROWCOUNT = 0 --Nothing got affected
- RETURN --exit the trigger
- UPDATE PurchaseOrderHeader
- SET SubTotal = SubTotal +
- (SELECT SUM(LineTotal)
- FROM inserted
- WHERE PurchaseOrderHeader.PurchaseOrderID
- = inserted.PurchaseOrderID)
- WHERE PurchaseOrderHeader.PurchaseOrderID IN
- (SELECT PurchaseOrderID FROM inserted);
Part of SQL Server Programming Best Practices
Section Triggers



LTD Social Sitings
Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.