在 INSTEAD OF INSERT 触发器执行插入后,FOR INSERT 触发器是否会触发?

发布于 2024-10-21 04:25:02 字数 170 浏览 2 评论 0原文

标题几乎说明了一切...我有一个带有 INSTEAD OF INSERT 触发器的表,它检查某些内容并在一切正常时执行实际插入。它还具有一个 FOR INSERT、UPDATE 触发器,该触发器根据新插入行中的值更新其他表中的值。

我怀疑 FOR INSERT, UPDATE 触发器没有触发。为什么会这样呢?

The title pretty much says it all... I have a table with an INSTEAD OF INSERT trigger which checks certain things and does the actual insert if things are OK. It also has an FOR INSERT, UPDATE trigger which updates values in other tables based on the values in the newly inserted row.

I suspect that the FOR INSERT, UPDATE trigger is not firing. Why would this be?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

画尸师 2024-10-28 04:25:02

更新其他表中的值
基于新的值
插入行。

FOR INSERT 触发器中的“插入”表将包含 INSTEAD OF 触发器插入的值,NOT插入语句。例如,考虑以下脚本:

CREATE TABLE Test (
    id int IDENTITY NOT NULL,
    value varchar(20) NOT NULL,
    forTriggerValue char(1)  NULL
)
GO

CREATE TRIGGER forTrigger
ON Test
AFTER UPDATE 
AS 
    IF EXISTS (SELECT * FROM inserted WHERE value = 'MyNewValue')
    BEGIN
        UPDATE Test SET 
            forTriggerValue = 'A'
        FROM    inserted
        WHERE   Test.id IN (SELECT id FROM inserted)
    END
    ELSE IF EXISTS (SELECT * FROM inserted WHERE value = 'InsteadOfValue')
    BEGIN
        UPDATE Test SET 
            forTriggerValue = 'B'
        FROM    inserted
        WHERE   Test.id IN (SELECT id FROM inserted)
    END
    ELSE 
    BEGIN
        UPDATE Test SET 
            forTriggerValue = 'C'
        FROM    inserted
        WHERE   Test.id IN (SELECT id FROM inserted)
    END
GO

CREATE TRIGGER insteadOfTrigger
ON Test
INSTEAD OF  UPDATE 
AS 
    UPDATE Test SET 
        value = 'InsteadOfValue'
    FROM    inserted
    WHERE   Test.id IN (SELECT id FROM inserted)
GO

INSERT INTO Test (value) VALUES ('MyValue')
GO
UPDATE Test SET value = 'MyNewValue' WHERE value = 'MyValue'
GO

SELECT * FROM Test
GO

您的“forTriggerValue”将是“B”,而不是“A”。

which updates values in other tables
based on the values in the newly
inserted row.

The "inserted" table in the FOR INSERT trigger will contain the values inserted by your INSTEAD OF trigger, NOT your insert statement. For example, consider the following script:

CREATE TABLE Test (
    id int IDENTITY NOT NULL,
    value varchar(20) NOT NULL,
    forTriggerValue char(1)  NULL
)
GO

CREATE TRIGGER forTrigger
ON Test
AFTER UPDATE 
AS 
    IF EXISTS (SELECT * FROM inserted WHERE value = 'MyNewValue')
    BEGIN
        UPDATE Test SET 
            forTriggerValue = 'A'
        FROM    inserted
        WHERE   Test.id IN (SELECT id FROM inserted)
    END
    ELSE IF EXISTS (SELECT * FROM inserted WHERE value = 'InsteadOfValue')
    BEGIN
        UPDATE Test SET 
            forTriggerValue = 'B'
        FROM    inserted
        WHERE   Test.id IN (SELECT id FROM inserted)
    END
    ELSE 
    BEGIN
        UPDATE Test SET 
            forTriggerValue = 'C'
        FROM    inserted
        WHERE   Test.id IN (SELECT id FROM inserted)
    END
GO

CREATE TRIGGER insteadOfTrigger
ON Test
INSTEAD OF  UPDATE 
AS 
    UPDATE Test SET 
        value = 'InsteadOfValue'
    FROM    inserted
    WHERE   Test.id IN (SELECT id FROM inserted)
GO

INSERT INTO Test (value) VALUES ('MyValue')
GO
UPDATE Test SET value = 'MyNewValue' WHERE value = 'MyValue'
GO

SELECT * FROM Test
GO

Your "forTriggerValue" will be 'B', not 'A'.

德意的啸 2024-10-28 04:25:02

检查服务器上的“允许触发器触发其他人”设置。在 SSMS 中,右键单击服务器并选择属性。然后查看“高级”选项卡。

在此处输入图像描述

Check the "Allow Triggers to Fire Others" setting on the server. In SSMS, right click on the Server and choose Properties. Then look at the Advanced tab.

enter image description here

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