创建日期和上次修改日期触发器
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了创建日期和上次修改日期触发器相关的知识,希望对你有一定的参考价值。
First, create two new fields in your table "Created", and "LastModified" as "datetime" fields, being sure to allow nulls. Then run this code as a query to create triggers that will update the appropriate field when a record is modified, or a record is inserted. This proves very useful when you're dealing with a huge database.In the code below replace [TableName] with your actual table name, and replace [UniqueID] with a unique ID field name in your 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]
以上是关于创建日期和上次修改日期触发器的主要内容,如果未能解决你的问题,请参考以下文章