Automatic Create and LastModified Dates

Submitted by:Andery Smith

Date added:06 September, 2012

Category:SQL

MS SQL triggers for automatic Create and LastModified Dates

Tags: sql triggers

Code Snippet:

    /*
<TableName>
- Name of the selected table
<UniqueId>
- Primary Key of the selected table
*/

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>

GO
 
 

Comments