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

LessThanDot

Data Management

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.

Your profile

    Search

    XML Feeds

    Google Ads

    « Dealing with the Column name 'TEXT()' contains an invalid XML identifier as required by FOR XML; '('(0x0028) is the first character at fault errorSQL Advent 2012 Day 3: Sargable Queries »
    comments

    This is day four of the SQL Advent 2012 series of blog posts. Today we are going to look at triggers. Triggers are a great way to keep your database in a consistent state. There are two types of triggers, DML triggers and DLL triggers. DML triggers respond to Data Manipulation Statements (Insert, Delete, Update) DDL triggers respond to Data Definition Language events.

    Some things that DML triggers are used for:

    • Keeps your databases from having wrong data by doing checks that can't be handled with constraints
    • Filling in values that are not supplied and can't be handled through default constraints since these don't fire on updates
    • Calculation summary values and updates the summary table with that value
    • Used as a mechanism to maintain an audit trail for DML statements

    Some things that DDL triggers are used for:

    • Automatically add columns to a table if they were not added, for example LastUpdated and InsertedBy columns
    • Notify a DBA when a database has been created, dropped or altered
    • Used as a mechanism to maintain an audit trail for DDL statements, capture every time an object has been created, dropped or altered and by who

    Most common mistake people make when first starting writing triggers is that they write it in such a way that it will only work if you insert/update/delete one row at a time. A trigger fires per batch not per row, you have to take this into consideration otherwise your DML statements will blow up. How to do this is explained in this post Best Practice: Coding SQL Server triggers for multi-row operations, there is no point recreating that post here.

    Another problem that I see is that some people think a trigger is SQL Server's version of crontab, you will see code that sends email, kicks off jobs, runs stored procedures. This is the wrong approach, a trigger should be lean and mean, it should execute as fast as possible, if you need to do some additional things then dump some data from the trigger into a processing table and then use that table to do your additional tasks. Don't use triggers as a messaging system either, SQL Server comes with Service Broker, use that instead. Triggers might look like hammers to some people but I guarantee you not everything is a nail....

    You could end up with a real difficult thing to debug, one trigger that kicks off other triggers, now have fun debugging the trigger hell you got yourself into....or worse debug this mess if you inherited this....this is like the GOTO spaghetti code of databases.

    Since triggers work besides the scenes you might spend hours debugging something only to find out that a trigger modified the value

    One thing I always find interesting is when someone sees two n rows affected statements when they only did one insert, you know a person like that has not been exposed to triggers yet

    Some people will say that you don't need triggers for anything and that they do more harm than good, I myself don't agree with that, triggers have a place but they should not be abused and overused, the same can be said of views

    What is your opinion, are triggers needed or are they not needed?




    That is all for day four of the SQL Advent 2012 series, come back tomorrow for the next one, you can also check out all the posts from last year here: SQL Advent 2011 Recap

    About the Author

    User bio imageDenis has been working with SQL Server since version 6.5. Although he worked as an ASP/JSP/ColdFusion developer before the dot com bust, he has been working exclusively as a database developer/architect since 2002. In addition to English, Denis is also fluent in Croatian and Dutch, but he can curse in many other languages and dialects (just ask the SQL optimizer) He lives in Princeton, NJ with his wife and three kids.
    Social SitingsTwitterFacebookLinkedInHomePageFlickrLTD RSS Feed
    2889 views
    InstapaperVote on HN

    2 comments

    Comment from: alzdba [Member] Email
    alzdba 100% with you, Denis.

    Another thing to keep in mind is : A trigger is "in transaction". If the trigger fails, you whole transaction fails.
    Nice example: simple replication trigger abuse: Trigger inserts into a linked server table. That (linked) server is being taken down or is locked for whatever reason.
    You cannot insert new rows to your table ...
    (unless you disable the trigger)
    Case closed.
    12/04/12 @ 06:40
    Comment from: Koen Verbeeck [Member] Email
    Triggers are needed, but with caution. When I started to use triggers a few years back, I burned myself with the "one row at a time" approach :)

    I like your idea of a DDL trigger that adds columns if they were not included in the table definition. This might come in handy someday.
    12/11/12 @ 04:56

    Leave a comment


    Your email address will not be revealed on this site.

    To mislead the spambots.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)