trigger on Multiple columns

  • Is it possible to have a trigger on a single table (column2, column3, column5 & column9) and if so I would have to used the "columns_updated" method.

  • kd11 (5/4/2015)


    Is it possible to have a trigger on a single table (column2, column3, column5 & column9) and if so I would have to used the "columns_updated" method.

    All triggers are on a single table. Now what is the question here?

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Let me re-word that, I want to fire a trigger when column2, column3, column5 and column9 are updated/insert (not the other six columns) so do I need to use the "column_update" statement.

  • kd11 (5/4/2015)


    Let me re-word that, I want to fire a trigger when column2, column3, column5 and column9 are updated/insert (not the other six columns) so do I need to use the "column_update" statement.

    column_update is very misleading, since it just checks if the column name was included in the DML command, and not whether the values actually changed.

    you might need to handle NULLS as well, so this is just to provide an example:

    the best way is to check by comparign the INSERTED virtual table to teh DELETED virtual table, on a column basis.

    ie

    IF EXISTS(SELECT * FROM INSERTED i INNER JOIN DELETED d ON i.PKColumn = d.PKColumn WHERE i.Name <> d.Name OR i.Address1 <> d.Address1)

    BEGIN

    --log some extra data? send an email? do something

    END

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • kd11 (5/4/2015)


    Let me re-word that, I want to fire a trigger when column2, column3, column5 and column9 are updated/insert (not the other six columns) so do I need to use the "column_update" statement.

    An insert always affects all columns of a table.

    Could you be more specific about what you're trying to do?

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply