Efficient trigger type

  • From logical_io point of view what kind of trigger For Update or After Update is more beneficial when doing bulk update?

    I mean, trigger was created on tableA and update column on tableB. For 1 record updated in tableA there is certainly no difference between 2 mentioned trigger types. But in case multiple records updated in tableA (say, bulk)- what is better? Or there is no difference (like number of round trips from tableA to tableB)? Thanks

  • No difference between FOR and AFTER

    From Books online

  • AFTER triggers are executed after the action of the INSERT, UPDATE, or DELETE statement is performed. Specifying AFTER is the same as specifying FOR, which is the only option available in earlier versions of SQL Server. AFTER triggers can be specified only on tables.
  • A trigger fires only once, no matter how many rows are updated so you should write your trigger in an efficient way and write it to handle any number of records affected.

    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
  • May be I am wrong but the difference is that (from BOL) "AFTER triggers are never executed if a constraint violation occurs" and FOR trigger doesn't care. But, probably, from effeciency point of view there is no difference. Thanks

  • AFTER and FOR are synonyms of each other. They create the same trigger. The only types of triggers available in SQL 2000 are AFTER and INSTEAD OF

    You can use the FOR clause to specify when a trigger is executed:

    • AFTER

      The trigger executes after the statement that triggered it completes. If the statement fails with an error, such as a constraint violation or syntax error, the trigger is not executed. AFTER triggers cannot be specified for views, they can only be specified for tables. You can specify multiple AFTER triggers for each triggering action (INSERT, UPDATE, or DELETE). If you have multiple AFTER triggers for a table, you can use sp_settriggerorder to define which AFTER trigger fires first and which fires last. All other AFTER triggers besides the first and last fire in an undefined order which you cannot control.

      AFTER is the default in SQL Server 2000. You could not specify AFTER or INSTEAD OF in SQL Server version 7.0 or earlier, all triggers in those versions operated as AFTER triggers.

    • INSTEAD OF

      The trigger executes in place of the triggering action. INSTEAD OF triggers can be specified on both tables and views. You can define only one INSTEAD OF trigger for each triggering action (INSERT, UPDATE, and DELETE). INSTEAD OF triggers can be used to perform enhance integrity checks on the data values supplied in INSERT and UPDATE statements. INSTEAD OF triggers also let you specify actions that allow views, which would normally not support updates, to be updatable.

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

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