更新两行更新触发器问题

时间:2011-04-20 08:06:44

标签: sql sql-server

我按名称触发了一个表:[OfficeAutomation_Letter]  更新的表格,每行更新另一次  但是当更新命令我运行到两行时  会触发错误  我需要解释过道的每一行如何发展

我的触发器是:

ALTER TRIGGER [dbo].[Letter_UPD] ON [dbo].[OfficeAutomation_Letter] 
FOR UPDATE
AS

Declare @LetterID int
SELECT @LetterID = ID From Inserted

BEGIN           
    UPDATE OfficeAutomation_TempLetter 
    SET Archived = (Select Archive From Inserted ),
    OriginalNumber = (Select OriginalNumber From Inserted )
    WHERE LetterID = @LetterID          
END

更新我的命令是:

update OfficeAutomation_Letter set OriginalNumber=100 where ID in (3,4)

访问错误是:

    Subquery returned more than 1 value. This is not permitted when
 the subquery follows =, !=, <, <= , >, >= or when the subquery 
is used as an expression.

1 个答案:

答案 0 :(得分:3)

触发器每个语句运行一次,而不是每行一次,插入和删除的伪表包含所有受影响的行。

ALTER TRIGGER [dbo].[Letter_UPD] ON [dbo].[OfficeAutomation_Letter] 
FOR UPDATE
AS
    UPDATE otl
    SET Archived = i.Archive,
    OriginalNumber = i.OriginalNumber
    FROM
      OfficeAutomation_TempLetter otl
         inner join
      inserted i
         on
            otl.LetterID = i.ID

所以,在原始触发器中,我们有:

SELECT @LetterID = ID From Inserted

将分配一个受影响的行的ID(哪一个,谁知道? - 未定义

然后

SET Archived = (Select Archive From Inserted ),
OriginalNumber = (Select OriginalNumber From Inserted )

这是您看到的错误的实际原因,因为右侧的选择将返回多行。