标签:style blog color 使用 io strong for ar
|
虚拟表Inserted |
虚拟表Deleted |
在表记录新增时 |
存放新增的记录 |
不存储记录 |
修改时 |
存放用来更新的新记录 |
存放更新前的记录 |
删除时 |
不存储记录 |
存放被删除的记录 |
CREATE TRIGGER trigger_delOnWork ON T_OnWork_info FOR DELETE AS DECLARE @name varchar(10) DECLARE @loginDate varchar(10) DECLARE @loginTime varchar(10) DECLARE @logoutDate varchar(10) DECLARE @logoutTime varchar(10) DECLARE @comName varchar(10) SELECT @name =(SELECT NAME FROM deleted ) SELECT @loginDate =(SELECT loginDate FROM deleted )-----deleted表用于存储T_OnWork表中已经删除的记录 SELECT @loginTime =(sELECT loginTime FROM deleted ) SELECT @comName =(SELECT comName FROM deleted ) SET @logoutDate=convert(varchar(10),getdate(),101) SET @logoutTime =convert(varchar(10),getdate(),108) if (SELECT COUNT(*) FROM deleted )=1 BEGIN INSERT INTO T_Work_info(Name,loginDate,loginTime,logoutDate,logoutTime,comName) VALUES(@name,@loginDate ,@loginTime ,@logoutDate,@logoutTime,@comName ) UPDATE T_Admin_info SET IsOn='False' WHERE Name=(SELECT Name FROM deleted ) ROLLBACK TRAN --事务回滚 END怎么样,触发器很简单吧,大家可以尝试着用用,触发器+事务的回滚机制,很容易地做到了为代码减负
SQL Server -为代码减负之触发器,布布扣,bubuko.com
标签:style blog color 使用 io strong for ar
原文地址:http://blog.csdn.net/tgbsqliuying/article/details/38736849