MS SQL 服务器的插入触发器

On Insert Trigger For MS SQL Server

我想在数据插入 NFI 时将数据插入到我的 table sn - 我已将以下触发器添加到我的数据库,但在插入 NFI 时数据没有插入到 sn。

我需要更改什么才能成功运行?

CREATE TRIGGER UpdateData ON nfi
FOR INSERT
AS

INSERT INTO SN
        (esn,ern)
    SELECT
        esn,ern
        FROM inserted
        where esn not in (select esn from nfi)

go

触发触发器时,数据已存在于 nfi table 中。 where esn not in (select esn from nfi) 子句确保不会将任何内容插入 sn.

当您创建触发器且未明确指定 AFTERINSTEAD OF 时,则 AFTER 是默认值。从 CREATE TRIGGER 文档中,您可以阅读:

FOR | AFTER AFTER specifies that the DML trigger fires only when all operations specified in the triggering SQL statement have launched successfully. All referential cascade actions and constraint checks must also succeed before this trigger fires.

AFTER is the default when FOR is the only keyword specified.