Best Practices
Triggers are really just stored procedures in disguise. They have a few additional capabilities and nuances, but generally speaking, you take the same approach to writing a trigger as you would to build a stored procedure. A few additional thoughts:
Make sure your triggers allow for the possibility that more than one row could be altered at once. Triggers that work fine with single-row operations often break when multirow operations come their way. Not allowing for multirow updates is the single most common error that trigger neophytes make.
Begin each trigger by checking @@ROWCOUNT to see whether any rows have changed. If none have, exit immediately because there's nothing for the trigger to do.
Use the UPDATE() and COLUMNS_UPDATED() ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access