T-SQL and transaction flow - from first to last

前端 未结 3 1418
说谎
说谎 2021-01-07 13:33

Let\'s say I have table TabA with columns:

  • col1 - primary key (but not identity)

  • col2 - foreign key

  • col3 -

3条回答
  •  甜味超标
    2021-01-07 13:55

    General order of execution for INSERT, UPDATE, DELETE statements:

    • Enforce all table- and row-level constraints. Note that you have zero control over the order in which these constraints are checked.
    • If an INSTEAD OF trigger exists for the statement being executed, execute it.
    • Execute all apropriate AFTER triggers, in undefined order, with the following exceptions:
      • IF an after trigger has been specified as the first or last to be executed via sp_settriggerorder, execute those at the appropriate point, with any remaining triggers executed in undefined order.

    You may have only 1 INSTEAD OF trigger (per action: INSERT, UPDATE or DELETE). That trigger will always get executed before any AFTER triggers, since it executes in lieue of the corresponding INSERT, UPDATE or DELETE statement.

    AFTER triggers are always executed, oddly enough, AFTER the data modification statement executes.

    NOTE: If you have an INSTEAD OF trigger, it is unclear to me, not having spent any real amount of time fussing with INSTEAD OF triggers, whether or not table/row constraints are enforced prior to execution of the INSTEAD OF trigger. That's something you might want to experiment with.

提交回复
热议问题