time stamp

  • I'm trying to time stamp a date field every time a row is inserted or updated. I put together the trigger below and it works but instead of time stamping just the row being inserted or updated it time stamp each and every row. Can someone please explain to me what I'm doing wrong.

    Ayisat

    CREATE TRIGGER updatedby ON dbo.data_info

    FOR INSERT

    AS

    UPDATE dbo.data_info SET date_entered = getdate()

    Ayisat Idris


    Ayisat Idris

  • The UPDATE statement has no restrictions as it is currently written and that's why you are seeing every row changed.

    Take a look at the inserted table which is available to us whenever we have an INSERT or UPDATE trigger. If you do a join on the primary keys, you should be able to only update the records which were changed and caused the trigger to fire.

    K. Brian Kelley

    bkelley@sqlservercentral.com

    http://www.sqlservercentral.com/columnists/bkelley/

    K. Brian Kelley
    @kbriankelley

  • Thanks it is working correctly now.

    Ayisat

    The UPDATE statement has no restrictions as it is currently written and that's why you are seeing every row changed.

    Take a look at the inserted table which is available to us whenever we have an INSERT or UPDATE trigger. If you do a join on the primary keys, you should be able to only update the records which were changed and caused the trigger to fire.

    K. Brian Kelley

    bkelley@sqlservercentral.com

    http://www.sqlservercentral.com/columnists/bkelley/

    [/quote]

    Ayisat Idris


    Ayisat Idris

Viewing 3 posts - 1 through 2 (of 2 total)

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