问题
I need to know about how the trigger execution happens for the below scenario.
I have 20 records in a table and I have an AFTER INSERT, UPDATE
trigger on that table. When I'm updating all the records in that table using a MERGE
or batch update statement, how will the trigger execute?
Does it execute for each row by row?
Or is it executing once per a batch (once for all 20 records)?
If it is execute once per batch do we need to write a loop inside the trigger to perform a task for each row?
回答1:
Triggers in SQL Server always execute once per batch - there's no option for "for each row" triggers in SQL Server.
When you mass-update your table, the trigger will receive all the updated rows at once in the inserted
and deleted
pseudo tables and needs to deal with them accordingly - as a set of data - not a single row
来源:https://stackoverflow.com/questions/32324871/sql-server-trigger-execution-in-batch-updating