If Update() in a trigger

  • I have a question that I thought I knew the answer to but I am now not sure. When in a trigger, suppose there are 10 rows in the inserted table. 3 rows have one column(call it A) updated and 7 rows have another column (call it B) updated. If code is entered in the trigger that states If Updated(A) begin ... end will the actions between the begin and end be executed on all 10 rows or only the 3 with the column A updated?

  • A trigger fires once only for an operation. All the rows affected by the insert/update/delete will be in the inserted and/or deleted tables.

    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
  • dean.giberson 64357 (9/17/2012)


    I have a question that I thought I knew the answer to but I am now not sure. When in a trigger, suppose there are 10 rows in the inserted table. 3 rows have one column(call it A) updated and 7 rows have another column (call it B) updated. If code is entered in the trigger that states If Updated(A) begin ... end will the actions between the begin and end be executed on all 10 rows or only the 3 with the column A updated?

    In a single insert or update of all 10 rows for the above, all 10 rows will respond to the IF UPDATED(A) because IF UPDATED() simply checks to see if anything in the whole column for the whole statement was affected.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Thanks for the reply. So I am clear on the answer, whatever the processing is between the begin and end will be executed because the system is only checking if column A has been updated for any of the rows in the inserted table so even if only one of the 10 rows had column A updated all 10 rows would still be processed. Am I correct?

  • Not quite...

    It's checking whether ColumnA was one of the columns specified in the SET clause of the update statement.

    UPDATE Tbl SET ColumnA = ColumnA

    If you had an update trigger on Tbl checking for UPDATE(ColumnA), it would be true for that update statement, even though no rows have the value of ColumnA changed.

    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
  • Gotcha, not CHANGED just UPDATED.

  • Yup. You want changed, you need to compare the columns in the inserted and deleted tables.

    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 7 posts - 1 through 6 (of 6 total)

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