触发器在SQL SERVER中为多行更新提供错误

时间:2015-07-06 12:10:00

标签: sql-server triggers

我有一个问题,当我创建一个在一行上正常工作的触发器但是当我在多行上应用它时它给了我这个错误

  

当子查询遵循=,!=,<,< =,>,> =或子查询用作表达式时,不允许这样做

触发器是:

Create TRIGGER AccountChange
ON Customer
AFTER UPDATE
AS
BEGIN
    IF(UPDATE(TotalSales))
    BEGIN
        DECLARE @TotalSales MONEY, @AccountNumber NVARCHAR(20),
                @TotalSalesOld MONEY, @customText1 NVARCHAR(50),
                @AccountTypeId int

        SET @TotalSales = (SELECT TotalSales FROM INSERTED)
        SET @TotalSalesOld = (SELECT TotalSales FROM DELETED)
        SET @AccountNumber = (SELECT AccountNumber FROM INSERTED)
        SET @AccountTypeId = (SELECT AccountTypeId FROM INSERTED)

        IF(@TotalSales BETWEEN 0 AND 5000)
           UPDATE Customer 
           SET AccountTypeID = 8 
           WHERE AccountNumber = @AccountNumber
   END
END

我只是希望它可以处理任意数量的插入行,因为我已经安排了每次都有多次插入/更新的计划连接。

提前致谢。

1 个答案:

答案 0 :(得分:1)

您的触发器应如下所示,以处理包含多个受影响行的class

UPDATE

一个单一的,非常简单明了的基于集合的陈述 - 这就是全部!