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]