SQL Server -为代码减负之触发器
时间:2022-03-13 22:55
- 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