Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Community Wiki

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.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

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

Navigation

Google Ads

General best practices for SQL Server triggers

From Wiki

Jump to: navigation, search

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


  1. CREATE TRIGGER NewPODetail2
  2. ON Purchasing.PurchaseOrderDetail
  3. AFTER INSERT AS
  4. IF @@ROWCOUNT = 0 --Nothing got affected
  5.    RETURN  --exit the trigger
  6.    
  7.    UPDATE PurchaseOrderHeader
  8.    SET SubTotal = SubTotal +
  9.       (SELECT SUM(LineTotal)
  10.       FROM inserted
  11.       WHERE PurchaseOrderHeader.PurchaseOrderID
  12.        = inserted.PurchaseOrderID)
  13.    WHERE PurchaseOrderHeader.PurchaseOrderID IN
  14.       (SELECT PurchaseOrderID FROM inserted);


Part of SQL Server Programming Best Practices

Section Triggers

653 Rating: 0.0/5 (0 votes cast)