sqlserver 触发器 怎么获取更新前的值
更新的动作你可以分2步理解,先delete ,再insert
所以,前的值在 deleted里
后的值在 inserted里
create trigger t_ATDATEState_update
on list
for update
as
if update(ATDATEState)
begin
declare @b_ATDATEState nvarchar(200) --修改之前的
declare @S_ATDATEState nVARCHAR(200) --修改之后的
declare @S_id INT
SELECT @S_ATDATEState= ATDATEState FROM INSERTED --从更新后的副本表(临时表)里面 获得要修改后的状态
select @S_id=id,@b_ATDATEState=ATDATEState from deleted --从之前删掉的临时表里面获取原来的值
if @S_ATDATEState is null
begin
set @S_ATDATEState='0'
end
if @b_ATDATEState is null
begin
set @b_ATDATEState='0'
end
--如果修改前为2,3,4 降低了状态 向下修改 并且 修改后的状态和修改前不一样
if ((convert(int,@b_ATDATEState)>=2 and convert(int,@S_ATDATEState)<convert(int,@b_ATDATEState)) and (@b_ATDATEState<>@S_ATDATEState))
begin
insert into dbo.operate(c_id,before_ATDATEState,after_ATDATEState,o_ip,o_type)
values(@S_id,@b_ATDATEState,@S_ATDATEState,'','状态不正常,向下降低了')
end
if(convert(int,@b_ATDATEState)<2) and ( convert(int,@S_ATDATEState)>=2)
begin
insert into dbo.operate(c_id,before_ATDATEState,after_ATDATEState,o_ip,o_type)
values(@S_id,@b_ATDATEState,@S_ATDATEState,'','状态升高')
end
end