[In addition to blogging, I am also now using Twitter for quick updates and to share links. Follow me at: twitter.com/pabloperalta] Agility - CREATED DATE & LAST MODIFIED DATE TRIGGERS - Pablo Peralta's Blog
Tuesday, October 07, 2008 10:41 AM pablop

Agility - CREATED DATE & LAST MODIFIED DATE TRIGGERS

It is common that for certain tables in our DBs we include the creation date and last modification date for every row.

This implies that we have to manually create a trigger for that.

I found the following script  that saves our time and avoid to write it manually.

Simply:

-          Create the columns in our table (here named ‘Created’ y  ‘LastModified’) respectively.

-          Find & replace ‘TableName’ and  ‘UniqueID’.

That is. Simple, clear :).

The script:

CREATE TRIGGER tr[TableName]CreateDate ON [TableName]

FOR INSERT

AS

UPDATE [TableName] SET [TableName].Created=getdate()

FROM [TableName] INNER JOIN Inserted ON [TableName].[UniqueID]= Inserted.[UniqueID]

 

GO

 

CREATE TRIGGER tr[TableName]LastModifiedDate ON [TableName]

FOR UPDATE

AS

UPDATE [TableName] SET [TableName].LastModified=getdate()

FROM [TableName] INNER JOIN Inserted ON [TableName].[UniqueID]= Inserted.[UniqueID]

Comments

No Comments

Leave a Comment

(required) 
(required) 
(optional)
(required)