“最大允许触发深度”在 SQL Server 中?
我知道“允许触发器触发其他触发器”服务器设置,该设置允许触发器采取的操作触发另一个触发器(或不触发),据我了解,我唯一的选项是 True (允许触发器触发其他触发器,这可能导致无休止的递归)或 False(触发器采取的操作不会触发任何其他触发器,这可能会导致意外结果或不一致的数据)。
有没有办法在 SQL Server 中强制执行“最大触发深度”?我正在使用 2008,如果它很重要的话,尽管我不知道任何版本上的功能(或在任何其他 RDBMS 中,就此而言,尽管我的知识确实有限)。例如,这就是我想要的:
- 将“最大触发深度”设置为“2”。
- 我在 table1 中插入一行
- table1 上的触发器插入 table2
- Table2 上的触发器插入 table3
- table3 上有一个触发器将插入 table4,但由于最大深度为 2,触发器不会运行(小于理想,但与 SQL Server 上当前的“递归触发器 = False”行为一致),或者整个插入集回滚并失败,并显示“超出最大触发深度 (2) - 插入失败”消息之类的错误(理想
)有人知道这是否可能,或者是否有针对此行为的突出功能请求?如果我疯了并且这是一个糟糕的行为想法,我对此持开放态度,但我想知道为什么(无意的后果等)。
I know about "Allow Triggers to Fire Others" server setting that allows the action taken by a trigger to fire another trigger (or not), and as I understand it, my only options are True (allow trigger to fire other triggers, which may lead to unending recursion) or False (actions taken by triggers will not fire any other triggers, which may lead to unexpected results or inconsistent data).
Is there a way to enforce a "Maximum trigger depth" in SQL Server? I'm using 2008, if it matters, though I'm not aware of the feature on any version (or in any other RDBMS, for that matter, though my knowledge is admittedly limited). For example, here's what I'd like:
- Set the "Maximum trigger depth" to "2".
- I insert a row into table1
- A trigger on table1 inserts into table2
- A trigger on table2 inserts into table3
- There's a trigger on table3 that would insert into table4, but since the maximum depth is 2, either the trigger just doesn't get run (less ideal, but consistent with current "Recursive triggers=False" behavior on SQL Server), or the whole set of inserts is rolled back and fails with an error like "Maximum trigger depth (2) exceeded - insert failed" message (ideal)
Does anybody out there know if this is possible, or if there's an outstanding feature request for this behavior? If I'm crazy and this is a terrible idea for behavior, I'm open to that, but I'd like to know why (unintentional consequences, etc).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在触发器内部,您可以检查嵌套级别,并可选择返回:
请参阅此 博客文章 了解更多详细信息。
Inside a trigger, you can check the nesting level, and optionally return:
See this blog post for more details.