Posted By

codio_mwnci on 07/13/12


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

ringo380


ms sql triggers for automatic Create and LastModified Dates


 / Published in: SQL
 

ms sql triggers for automatic Create and LastModified Dates

  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  

You need to login to post a comment.