SQL Server 2005 - 触发循环?

发布于 2024-07-16 03:52:13 字数 98 浏览 4 评论 0原文

我是第一次使用触发器。

如果我通过同一个表上的更新触发器更新表中的字段,会引发循环吗? 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

简单 2024-07-23 03:52:14

此页面(搜索RECURSIVE_TRIGGERS)描述了一些可用于修改此行为的数据库设置。 此外,保护程序的一种方法是使用 UPDATE( ) 函数或 COLUMNS_UPDATED() 函数。

例如,如果您有一个包含 ABC 列的表,并且您需要 C< /code> 当B列中的值更新时自动更改,您可以保护触发器中的调用:

CREATE TRIGGER Whatever ON TableName AFTER UPDATE
AS
BEGIN
    IF UPDATE(B)
    BEGIN
        /* Update column C here */
    END
END

这样就可以避免当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 the UPDATE() function or the COLUMNS_UPDATED() function.

If, for example, you have a table with columns A, B, and C, and you want the value of C to change automagically when the value in column B is updated, you can protect the call in the trigger:

CREATE TRIGGER Whatever ON TableName AFTER UPDATE
AS
BEGIN
    IF UPDATE(B)
    BEGIN
        /* Update column C here */
    END
END

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).

凹づ凸ル 2024-07-23 03:52:14

您可以通过 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文