SQL Server 2005-触发循环? [英] SQL Server 2005 - Trigger Loop?

查看:74
本文介绍了SQL Server 2005-触发循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是第一次使用触发器。

I am using triggers for the first time.

如果我通过同一表上的更新触发器来更新表中的字段,是否会引发循环? sql server是否可以防范这种递归行为?

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?

谢谢

推荐答案

此页面(搜索 RECURSIVE_TRIGGERS )描述了一些可用于修改此行为的数据库设置。另外,一种保护过程的方法是使用 UPDATE() 函数或 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.

例如,如果您有一个表,该表的列 A B C ,并且您想要 C B 列中的值更新时自动更改,可以保护触发器中的调用:

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

这样,您可以避免在触发器更新了列 C 时递归调用触发器。 COLUMNS_UPDATED()也很有用,但我发现它很脆弱(依赖列的位置而不是列名)。

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

这篇关于SQL Server 2005-触发循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆