SQL Server触发器错误 - 子查询返回的值超过1

时间:2017-08-09 19:26:15

标签: sql-server triggers

好的,所以我真的卡住了,我的触发器导致了这个错误:消息512,级别16,状态1,过程Sofi_TERA_Trigger,第15行 子查询返回的值超过1。当子查询遵循=,!=,<,< =,>,> =或子查询用作表达式时,不允许这样做。

在开始时我认为这是我的“如果”,但我改变了它(在多个“AND”条件的地方使用“IN”)。我也找了我的错误,但它几乎与任何事情有关。

这可能是我看不到的非常简单的事情。这是我的触发器:

TRIGGER [dbo].[Sofi_TERA_Trigger]
   ON [dbo].ZZ
   AFTER UPDATE,INSERT
AS 
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    IF ((Select inserted.Statut from inserted) IN ('*CLOT*','*CLTT*','*CONF*'))
        BEGIN
            DECLARE @Id int;
            DECLARE @Matricule varchar(10);
            DECLARE @IdAction int;
            DECLARE @NumeroOF int; 
            SELECT @NumeroOF = inserted.Ordre from inserted;

            DECLARE OF_CURSOR CURSOR
                LOCAL STATIC READ_ONLY FORWARD_ONLY
            FOR
            SELECT Id,Log.Matricule,IdAction from Log inner join (select max(Id) as maxID,Matricule from LOG where Log.NumeroOF = @NumeroOF group by Matricule) maxID 
            on maxID.maxID = Log.Id where Log.NumeroOF = @NumeroOF;

            OPEN OF_CURSOR
            FETCH NEXT FROM OF_CURSOR INTO @Id,@Matricule,@IdAction
            WHILE @@FETCH_STATUS = 0
            BEGIN
                IF @IdAction!=13
                    BEGIN
                        IF @IdAction<=2
                        BEGIN
                            insert into Log(NumeroOF,Matricule,IdAction,Date,EstAdmin) values (@NumeroOF,@Matricule,13,GETDATE(),1);
                        END
                        ELSE
                        BEGIN
                            insert into Log(NumeroOF,Matricule,IdAction,Date,EstAdmin) values (@NumeroOF,@Matricule,2,GETDATE(),1);
                            insert into Log(NumeroOF,Matricule,IdAction,Date,EstAdmin) values (@NumeroOF,@Matricule,13,GETDATE(),1);
                        END
                    END              
                FETCH NEXT FROM OF_CURSOR INTO @Id,@Matricule,@IdAction
            END

            CLOSE OF_CURSOR;
            DEALLOCATE OF_CURSOR;
    END 
END

1 个答案:

答案 0 :(得分:2)

问题是该行:

IF((Select inserted.Statut from inserted) IN ('*CLOT*','*CLTT*','*CONF*'))

语法无效。你需要改变它:

IF EXISTS(SELECT 1 FROM inserted WHERE inserted.Statut IN ('*CLOT*','*CLTT*','*CONF*'))