1

我有以下用于行更新的简单触发器。它应该做的是更新更新行上的 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

任何人都可以提供一些提示吗?提前致谢!

干杯,亚历克斯

4

1 回答 1

3

您只需要构建基于集合的更新而不是基于单个变量的更新:

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
于 2013-10-15T03:40:20.213 回答