SQL update trigger only when column is modified

后端 未结 5 2119
野趣味
野趣味 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:51

    You want to do the following:

    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
    

    Please note that this trigger will fire each time you update the column no matter if the value is the same or not.

提交回复
热议问题