views:

156

answers:

1

Hi, I want to create a trigger that will update the column LastActivityDate to the current date when the Description column is updated, the problem is that all the rows are being updated, and I don't know how to make the where clause inside the trigger...

I need to make a trigger like this for other tables as well, like the Votes table...

I have:

CREATE TRIGGER test ON Articles
FOR INSERT, UPDATE  
AS
IF UPDATE(Description)
UPDATE Articles SET LastActivityDate = GETUTCDATE()

How would the where clause be?

+4  A: 

You can join to the inserted table to limit the update to the rows updated .

  CREATE TRIGGER test ON Articles
    FOR INSERT, UPDATE  
    AS
    IF UPDATE(Description)
    UPDATE a
    SET a.LastActivityDate = GETUTCDATE()
    from Articles a
    inner join inserted i
    on a.SomeIDField = i.SomeIDField
cmsjr