SQL Server Update触发器在批量更新时无法正常工作

时间:2013-10-14 23:15:05

标签: sql sql-server-2008 sql-server-2008-r2 triggers

我有以下简单的行更新触发器。它应该做的是更新更新行上的LastUpdatedDate列。它适用于单行更新,但不适用于从应用程序端启动的批处理行更新。

ALTER TRIGGER [gc].[T_UpdateObservation] ON [gc].[GCUR_OBSERVATION] 
  AFTER UPDATE
AS 
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- get the last id value of the record inserted or updated
    DECLARE @id INT
    SELECT @id = [ObservationId]
    FROM DELETED

    -- Update statements for trigger here
    UPDATE GCUR_OBSERVATION
    SET GCUR_OBSERVATION.LastUpdatedDate = getdate()
    WHERE [ObservationId] = @id

END

有人可以提供一些提示吗?提前谢谢!

干杯, 亚历

1 个答案:

答案 0 :(得分:3)

您只需要基于单个变量构建set-based updated而不是一个:

ALTER TRIGGER [gc].[T_UpdateObservation] 
ON [gc].[GCUR_OBSERVATION] 
AFTER UPDATE
AS 
BEGIN
    SET NOCOUNT ON;

    UPDATE g SET LastUpdatedDate = CURRENT_TIMESTAMP
    FROM gc.GCUR_OBSERVATION AS g
    -----^^ why is the schema missing from your version of the trigger?
    INNER JOIN inserted AS i
    ON g.ObservationID = i.ObservationID;
END
GO