ms sql triggers for automatic Create and LastModified Dates


/ Published in: SQL
Save to your folder(s)

ms sql triggers for automatic Create and LastModified Dates


Copy this code and paste it in your HTML
  1. /*
  2.  
  3. <TableName>
  4.  
  5. - Name of the selected table
  6.  
  7. <UniqueId>
  8.  
  9. - Primary Key of the selected table
  10.  
  11. */
  12.  
  13.  
  14. CREATE TRIGGER tr<TableName>CreateDate ON <TableName>
  15. FOR INSERT
  16. AS
  17. UPDATE <TableName> SET <TableName>.Created=getdate()
  18. FROM <TableName> INNER JOIN Inserted ON <TableName>.<UniqueId> = Inserted.<UniqueId>
  19.  
  20. GO
  21.  
  22. CREATE TRIGGER tr<TableName>LastModifiedDate ON <TableName>
  23. FOR UPDATE
  24. AS
  25. UPDATE <TableName> SET <TableName>.LastModified=getdate()
  26. FROM <TableName> INNER JOIN Inserted ON <TableName>.<UniqueId> = Inserted.<UniqueId>
  27.  
  28. GO

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.