SQL Server 2005 - 触发循环?
我是第一次使用触发器。
如果我通过同一个表上的更新触发器更新表中的字段,会引发循环吗? sql server 是否防范这种递归行为?
谢谢
I am using triggers for the first time.
If I update a field in a table by an update trigger on the same table, with this spark a loop? Does sql server guard against this recursive behavior?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此页面(搜索
RECURSIVE_TRIGGERS
)描述了一些可用于修改此行为的数据库设置。 此外,保护程序的一种方法是使用UPDATE( )
函数或COLUMNS_UPDATED()
函数。例如,如果您有一个包含
A
、B
和C
列的表,并且您需要C< /code> 当
B
列中的值更新时自动更改,您可以保护触发器中的调用:这样就可以避免当
C
列中的值更新时递归调用触发器由您的触发器更新。COLUMNS_UPDATED()
也很有用,但我发现它很脆弱(依赖于列的位置而不是列名称)。This page (search for
RECURSIVE_TRIGGERS
) describes some of the database settings you can use to modify this behavior. Also, one way to safeguard your procedures is to use either theUPDATE()
function or theCOLUMNS_UPDATED()
function.If, for example, you have a table with columns
A
,B
, andC
, and you want the value ofC
to change automagically when the value in columnB
is updated, you can protect the call in the trigger:This way you avoid calling the trigger recursively when column
C
is updated by your trigger.COLUMNS_UPDATED()
is also useful, but I find it to be fragile (relies on position of column instead of column name).您可以通过 RECURSION_TRIGGER 选项在数据库级别控制触发器的递归; 默认情况下它是关闭的。 即使打开此选项,触发器的嵌套层数也有 32 层的限制; 如果您的退出条件在达到 32 级限制之前没有停止递归,则所有更改都将回滚。
You can control recursion of triggers at the DB level via the RECURSION_TRIGGER option; it's turned off by default. Even if this option is turned on, there is a limit of 32 nested levels of triggers; all changes will be rolled back if your exit condition didn't stop the recursion before reaching the limit of 32 levels.