当前位置: 代码迷 >> Sql Server >> 怎么创建一个触发器,用于获取最近一次更新时间
  详细解决方案

怎么创建一个触发器,用于获取最近一次更新时间

热度:2   发布时间:2016-04-27 14:25:26.0
如何创建一个触发器,用于获取最近一次更新时间?
例如表a ,字段id,jf,rq
在字段jf变更时,更新rq字段为getdate(),作为此条记录的最近更新时间。
此表经常有更新操作,用触发器update数据会不会陷入无限循环呢??
有时候也会有其他方式更新rq字段

------解决方案--------------------
SQL code
--在更新jf的时候,一起更新rq就可以了,不需要触发器update 表aset jf='aaa',rq=getdate()where [email protected] --例如有条件
------解决方案--------------------
SQL code
--创建一个表,插入一条数据create table testupdate(    id int ,js int,rq datetime)insert into testupdate select 1,10,'2010-10-01'--创建触发器create trigger tri_testupdate on testupdate for update as    update testupdate set rq=getdate()   where id=(select id from deleted)go --更新一条记录update testupdate set js=12 where id=1--查看结果select * from testupdate/*id          js          rq----------- ----------- -----------------------1           12          2012-01-18 20:12:16.000*/
  相关解决方案