• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 347
  • Last Modified:

Trigger to set mod date for SQL 2005 table

I would like to write a SQL 2005 trigger to set the value of a last mod date column in any row that is inserted or updated in a specific table. Is this possible? I can create a trigger, but I don't know how to change the data for the inserted/updated row.
0
Paracom_Inc
Asked:
Paracom_Inc
  • 3
3 Solutions
 
dbbishopCommented:
CREATE TRIGGER dbo.updateTimeSatmp ON dbo.myTable FOR INSERT, UPDATE AS
UPDATE myTable
SET LastUpdated = GETDATE()
FROM myTable t1 INNER JOIN inserted t2 ON t1.PKcolumn = t2.PKColumn
0
 
YveauCommented:
when you have a column 'ModDate' it can be done by the trigger:

create trigger MyTrigger on Mytable
for update
as
begin
    update table Mytable
    set ModDate = getdate
    where inserted.ID = Mytable.ID -- match the record(s) on the PK fields
end

hope this helps ...
0
 
YveauCommented:
sorry, typo !
... getdate is a function ofcourse, so you need 'getdate()'

0
 
MrRobotCommented:
for insertion, you can give the default value of "getdate()", so when a new row is added, the field will take the date value automatically.
0
 
YveauCommented:
Glad I could be of any help !
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now