Results 1 to 3 of 3

Thread: Simple update Trigger with system date

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2001
    Location
    NY
    Posts
    89

    Simple update Trigger with system date

    I am new to using triggers, so I wanted to make sure this made sense before I made this live.

    I want a field on my table to be updated with the current date every time the record is updated.

    I created the following trigger on the table...
    Code:
    CREATE TRIGGER testUpdate ON [dbo].[testTable] 
    FOR UPDATE
    AS 
    UPDATE [dbo].[testTable] 
    SET 
     [dbo].[testTable].[updateMe] = getdate()
    FROM inserted i
    WHERE [testTable].[pkTest] = i.[pkTest]
    I am using SQL Server 2000 and have recursive triggers turned off. Do you see any major flaws with the way I am doing this?

    Thanks,
    AndyL
    Last edited by andyL; Feb 2nd, 2005 at 02:09 PM.

  2. #2
    Fanatic Member vb_dba's Avatar
    Join Date
    Jun 2001
    Location
    Somewhere aloft between the real world and insanity
    Posts
    1,016

    Re: Simple update Trigger with system date

    I don't see anything wrong with your trigger, but I do have a question about why you want to create a trigger just to update the date the record was last updated.

    How are you updating records in this table? Via a Stored Procedure? If so, then I would suggest updating the updateMe field to GetDate() in your stored procedure rather than incur the overhead of a trigger to do this for you.
    Chris

    Master Of My Domain
    Got A Question? Look Here First

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jul 2001
    Location
    NY
    Posts
    89

    Re: Simple update Trigger with system date

    Thanks,

    The table does not get updated by a stored procedure, but thank you for the suggestion. I am using the data from this table in another application and I need to track new records and updated records, so I added two fields DateCreate and DateUpdate. I set the default value for DateCreate to GetDate() and will be using this trigger to update the DateUpdate field. This table only has around 100 records which do not have much activity so I don't believe there will be much overhead.

    AndyL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width