SQL update trigger only when column is modified

后端 未结 5 2122
野趣味
野趣味 2020-12-02 08:19

By looking at other examples I\'ve come up with the following but it doesn\'t seem to work as I would like: I want it to only update the modified information if the Qt

5条回答
  •  北荒
    北荒 (楼主)
    2020-12-02 08:40

    One should check if QtyToRepair is updated at first.

    ALTER TRIGGER [dbo].[tr_SCHEDULE_Modified]
       ON [dbo].[SCHEDULE]
       AFTER UPDATE
    AS 
    BEGIN
    SET NOCOUNT ON;
        IF UPDATE (QtyToRepair) 
        BEGIN
            UPDATE SCHEDULE 
            SET modified = GETDATE()
               , ModifiedUser = SUSER_NAME()
               , ModifiedHost = HOST_NAME()
            FROM SCHEDULE S INNER JOIN Inserted I 
                ON S.OrderNo = I.OrderNo and S.PartNumber =    I.PartNumber
            WHERE S.QtyToRepair <> I.QtyToRepair
        END
    END
    

提交回复
热议问题