sqlsql-servert-sql

Triggers: Instead of / For Insert, which will trigger first?


I am trying to understand a Document of stored procedures and triggers where it inserts and updates a table with a trigger.

They gave me 2 trigger documents and there are two triggers for this particular table:

trig_table_ii and trig_table_ti.

Which will trigger first when table is inserted/updated?

In trig_table_ii ( INSTEAD OF INSERT ) there is an INSERT Statement here for that same TABLE. Does that mean trig_table_ii executes first?


Solution

  • None will fire when table is updated. FOR INSERT is the same of AFTER INSERT. This will fire after you finish inserting a record.

    INSTEAD OF INSERT will fire as a replacement of your insert. This trigger will ignore your original insert statement and execute whatever is there to execute.

    Lets say in your INSTEAD OF tigger you, for some reason, do not insert the record. So, FOR INSERT trigger will not be fired.

    Check here for more info: CREATE TRIGGER