SQL Server trigger execution in batch updating

寵の児 提交于 2019-12-23 13:19:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!