SQL 触发器中的 IN 语句错误

IN statement error in SQL triggers

我有一个触发器,可以根据条件向另一个 table 插入值, 在我的情况下,当我尝试使用 "INSERT INTO SELECT FROM" 插入多个值时,IF 条件中有一个 IN 语句 它 returns 以下错误 "Subquery returned more than 1 value. This is not permitted when..."

这是我的代码

    ALTER TRIGGER [dbo].[WentOut] ON  [dbo].[TB_EVENT_LOG]
       AFTER INSERT
    AS 
    BEGIN

    DECLARE @AID int

        SET NOCOUNT ON;
        IF ((SELECT  nTNAEvent from inserted) IN(1,2,3))    
        BEGIN

            INSERT INTO Cen.WentOutLog  (AutoID, nUserID, nOutDateTime,nOutTNAEvent ,nReaderID) select nEventLogIdn,nUserID, nDateTime,nTNAEvent, nReaderIdn from inserted 

        END
END

它适用于单值但不适用于多值插入"INSERT INTO SELECT FROM" 提前致谢

如 Mitch trigger 所述,并非每一行都触发。如果没记错,这就是你需要的。

ALTER TRIGGER [dbo].[WentOut]
ON [dbo].[TB_EVENT_LOG]
AFTER INSERT
AS
  BEGIN
      DECLARE @AID INT

      SET NOCOUNT ON;

      IF EXISTS(SELECT nTNAEvent
                FROM   inserted
                WHERE  nTNAEvent IN ( 1, 2, 3 ))
        BEGIN
            INSERT INTO Cen.WentOutLog
                        (AutoID,nUserID,nOutDateTime,nOutTNAEvent,
                         nReaderID)
            SELECT nEventLogIdn,nUserID,nDateTime,nTNAEvent,
                   nReaderIdn
            FROM   inserted
            WHERE  nTNAEvent IN ( 1, 2, 3 )
        END
      IF EXISTS(SELECT nTNAEvent
                FROM   inserted
                WHERE  nTNAEvent IN ( 0 ))
        BEGIN
        --statement
        END
  END