SQL Server触发器Trigger
前段时间,曾经写过一个例子,http://www.cnblogs.com/insus/articles/1916558.html是使用OUTPUT来记录更新前后的值,
现在可以使用SQL Server的触发器来实现。
演示,
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
--创建一个UPDATE触发器
CREATE TRIGGER [dbo].[tri_Member_Udate] ON [dbo].[Member]
FOR UPDATE
AS
BEGIN
--宣告变量
DECLARE @MemberId [int],@Name [nvarchar](100) ,@OldSalery [decimal](18, 6),@newSalery [decimal](18, 6)
--从deleted虚拟表取到旧值
SELECT @MemberId = [MemberId],@Name = [Name],@OldSalery = [Salary] FROM deleted
--从inserted虚拟表取到新值
SELECT @newSalery = [Salary] FROM inserted
--插入到目标表(临时表)中
INSERT INTO [dbo].[#SalaryReport] VALUES (@MemberId,@Name,@OldSalery,@newSalery)
END
CREATE TRIGGER [dbo].[tri_Member_Udate] ON [dbo].[Member]
FOR UPDATE
AS
BEGIN
--宣告变量
DECLARE @MemberId [int],@Name [nvarchar](100) ,@OldSalery [decimal](18, 6),@newSalery [decimal](18, 6)
--从deleted虚拟表取到旧值
SELECT @MemberId = [MemberId],@Name = [Name],@OldSalery = [Salary] FROM deleted
--从inserted虚拟表取到新值
SELECT @newSalery = [Salary] FROM inserted
--插入到目标表(临时表)中
INSERT INTO [dbo].[#SalaryReport] VALUES (@MemberId,@Name,@OldSalery,@newSalery)
END