sqlserver 触发器 怎么获取更新前的值
2个回答
展开全部
下文链接中,已经说明,请参空迅阅
展开全部
更新的动作你可以分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
所以,前的值在 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
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询