zoukankan      html  css  js  c++  java
  • SQL触发器Trigger

    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





  • 相关阅读:
    MeteoInfoLab脚本示例:Trajectory
    MeteoInfoLab脚本示例:闪电位置图
    MeteoInfoLab脚本示例:AVHRR HDF数据
    发布MeteoInfo 1.2.8
    切图,css注意事项
    C# Winform下一个热插拔的MIS/MRP/ERP框架15(窗体基类场景1)
    Xamarin Android Webview中JS调用App中的C#方法
    C# Winform下一个热插拔的MIS/MRP/ERP框架14(自动更新)
    C# Winform下一个热插拔的MIS/MRP/ERP框架13(窗体基类)
    RDLC报表的相关技巧四(报表内依照分组重置页码)
  • 原文地址:https://www.cnblogs.com/wupd2014/p/4944444.html
Copyright © 2011-2022 走看看