码迷,mamicode.com
首页 > 数据库 > 详细

SQL触发器Trigger

时间:2015-11-07 09:10:41      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:

  1. Create table Employee_Test(
  2. Emp_ID int Identity,
  3. Emp_name varchar(100),
  4. Emp_sal Decimal(10,2)
  5. )
  1. create table Employee_Test_Audit(
  2. Emp_ID int,
  3. Emp_name varchar(100),
  4. Emp_sal Decimal(10,2),
  5. Audit_Action varchar(100),
  6. Audit_Timestamp datetime
  7. )
After Inset Trigger
  1. CREATE TRIGGER trgAfterInsert ON [dbo].[Employee_Test]
  2. FOR INSERT
  3. AS
  4. declare @empid int;
  5. declare @empname varchar(100);
  6. declare @empsal decimal(10,2);
  7. declare @audit_action varchar(100);
  8. select @empid=i.Emp_ID from inserted i;
  9. select @empname=i.Emp_Name from inserted i;
  10. select @empsal=i.Emp_Sal from inserted i;
  11. set @audit_action=‘Inserted Record -- After Insert Trigger.‘;
  12. insert into Employee_Test_Audit
  13. (Emp_ID,Emp_Name,Emp_Sal,Audit_Action,Audit_Timestamp)
  14. values(@empid,@empname,@empsal,@audit_action,getdate());
  15. PRINT ‘AFTER INSERT trigger fired.‘
  16. GO
  1. CREATE TRIGGER trgAfterUpdate ON [dbo].[Employee_Test]
  2. FOR UPDATE
  3. AS
  4. declare @empid int;
  5. declare @empname varchar(100);
  6. declare @empsal decimal(10,2);
  7. declare @audit_action varchar(100);
  8. select @empid=i.Emp_ID from inserted i;
  9. select @empname=i.Emp_Name from inserted i;
  10. select @empsal=i.Emp_Sal from inserted i;
  11. if update(Emp_Name)
  12. set @audit_action=‘Updated Record -- After Update Trigger.‘;
  13. if update(Emp_Sal)
  14. set @audit_action=‘Updated Record -- After Update Trigger.‘;
  15. insert into Employee_Test_Audit(Emp_ID,Emp_Name,Emp_Sal,Audit_Action,Audit_Timestamp)
  16. values(@empid,@empname,@empsal,@audit_action,getdate());
  17. PRINT ‘AFTER UPDATE Trigger fired.‘
  18. GO
  1. CREATE TRIGGER trgAfterDelete ON [dbo].[Employee_Test]
  2. AFTER DELETE
  3. AS
  4. declare @empid int;
  5. declare @empname varchar(100);
  6. declare @empsal decimal(10,2);
  7. declare @audit_action varchar(100);
  8. select @empid=d.Emp_ID from deleted d;
  9. select @empname=d.Emp_Name from deleted d;
  10. select @empsal=d.Emp_Sal from deleted d;
  11. set @audit_action=‘Deleted -- After Delete Trigger.‘;
  12. insert into Employee_Test_Audit
  13. (Emp_ID,Emp_Name,Emp_Sal,Audit_Action,Audit_Timestamp)
  14. values(@empid,@empname,@empsal,@audit_action,getdate());
  15. PRINT ‘AFTER DELETE TRIGGER fired.‘
  16. GO
  1. CREATE TRIGGER trgInsteadOfDelete ON [dbo].[Employee_Test]
  2. INSTEAD OF DELETE
  3. AS
  4. declare @emp_id int;
  5. declare @emp_name varchar(100);
  6. declare @emp_sal int;
  7. select @emp_id=d.Emp_ID from deleted d;
  8. select @emp_name=d.Emp_Name from deleted d;
  9. select @emp_sal=d.Emp_Sal from deleted d;
  10. BEGIN
  11. if(@emp_sal>1200)
  12. begin
  13. RAISERROR(‘Cannot delete where salary > 1200‘,16,1);
  14. ROLLBACK;
  15. end
  16. else
  17. begin
  18. delete from Employee_Test where Emp_ID=@emp_id;
  19. COMMIT;
  20. insert into Employee_Test_Audit(Emp_ID,Emp_Name,Emp_Sal,Audit_Action,Audit_Timestamp)
  21. values(@emp_id,@emp_name,@emp_sal,‘Deleted -- Instead Of Delete Trigger.‘,getdate());
  22. PRINT ‘Record Deleted -- Instead Of Delete Trigger.‘
  23. end
  24. END
  25. GO





SQL触发器Trigger

标签:

原文地址:http://www.cnblogs.com/wupd2014/p/4944444.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!