trigger to determine if any column changed

Posted on 2014-08-20
Last Modified: 2014-09-09
Is there a way to write a trigger to determine when any column in a record has changed and then update a "edit_datetime" column?
Question by:HLRosenberger
    LVL 25

    Expert Comment

    by:Shaun Kline
    If you do not need to know if a column was updated, you can create a trigger that occurs instead of an update or after an update (but prior to the change committing). See this link:
    If you need to know a specific column changed you can use the UPDATE ( <column name> ) syntax.
    LVL 68

    Expert Comment

    Sure.  You'd probably want to generate the code using sys.columns rather than writing it by hand, but the general structure would look like this:

    CREATE TRIGGER <trigger_name>
    ON dbo.<table_name>
    UPDATE tn
    SET edit_datetime = GETDATE()
    FROM inserted i
    INNER JOIN deleted d ON
        d.key_col = i.key_col
    INNER JOIN dbo.<table_name> tn ON
        tn.key_col = i.key_col
        ISNULL(i.data_col1, '~~') <> ISNULL(d.data_col1, '~~') OR
        ISNULL(i.data_col2, '~~') <> ISNULL(d.data_col2, '~~') OR
        ISNULL(i.data_col3, -999) <> ISNULL(d.data_col3, -999) --OR ...other columns

    LVL 4

    Expert Comment

    by:Randy Knight, MCM
    Instead of rolling you own code, check out the SQL Server change tracking feature.
    LVL 1

    Accepted Solution

    I went with this:

    works great.   The SQL Server change tracking feature, I believe, tracks based on the logged in SQL user account;  I need to track based on my own "users" table; I was able to modify this  "Audit-Trail-Generator-for-Microsoft-SQL " to do this.
    LVL 1

    Author Closing Comment

    This is the solution I went with.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
    In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
    Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
    Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now