SQL Server execute trigger within trigger? [duplicate]

jgovgodb  于 2023-04-19  发布在  SQL Server
关注(0)|答案(1)|浏览(140)

This question already has answers here:

what does if we change database to Recursive Triggers Enabled :True? (2 answers)
Closed 19 hours ago.

I have a scenario where I have table A

This table has a trigger on it when deleting (After Delete)

Within this trigger if I call a delete statement on table A (DELETE FROM TABLE A etc), I would think that this would cause the trigger to run again? Wouldn't it just loop forever and keep calling the trigger? It is not doing that, everything seems to be working normal but I cannot seem to figure this out.

Thanks in advance for any help.

shstlldc

shstlldc1#

There are two types of recursion in SQL Server.

  1. Direct
  2. Indirect

Nesting/recursion of a trigger in SQL Server depends on the server trigger recursion and nested triggers configurations.
Wouldn't it just loop forever and keep calling the trigger?

No. the maximum nesting level is 32 for SQL Server.

You can read about these configurations in below links server trigger recursion Server Configuration Option and Configure the nested triggers Server Configuration Option

You can read more on this in action in below article

  1. Nested and Recursive Triggers in SQL Server
  2. Nested Triggers in SQL Server

A warning! You must ne careful using triggers especially in production environments. It may cause performance issues that may not be easy to figure out.

Hope this helps.

相关问题