Trigger SQL to UPDATE Only row being Updated

  • Need to create a Trigger to UPDATE a tables DATETIME column to current DATE/TIME any time an UPDATE takes place on the given row. 

    Unfortunately, this TRIGGER is UPDATING "ALL" ChangeDate values in the table.  Can someone examine and supply TRIGGER SQL to only UPDATE the ROW being UPDATED.

    CREATE TRIGGER trgu_TABLE_A ON TABLE_A FOR UPDATE

    AS

    BEGIN

      UPDATE TABLE_A SET ChangeDate = (SELECT GETDATE() FROM INSERTED)

    END

    Thx in advance

     

    BT
  • look up "TRIGGER INSERTED" on msdn.

  • You need a "where" clause on that update to only update the row in question.

  • You need to JOIN the inserted table to the updated table using the primary key, e.g.

    UPDATE a

    SET ChangeDate = GETDATE()

    FROM TABLE_A a

    INNER JOIN inserted i

    ON i.[ID] = a.[ID]

    Far away is close at hand in the images of elsewhere.
    Anon.

  • CREATE TRIGGER [trgu_TABLE_A] ON [dbo].[TABLE_A] AFTER UPDATE, INSERT

    AS

    BEGIN

     UPDATE TABLE_A SET ChangeDate = GETDATE()

      FROM inserted i INNER JOIN TABLE_A c ON i.ID = c.ID

    END

    BT

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply