日期:2014-05-18 浏览次数:20583 次
CREATE TRIGGER UpDateStateTrigger ON commonauditorlog AFTER INSERT BEGIN IF NEW.AuditorRes > 0 UPDATE commonapplytable set State = State+1 where ApplyID = NEW.ApplyID and State != -1 ELSE UPDATE commonapplytable set State = -1 where ApplyID = NEW.ApplyID END go
------解决方案--------------------
你这里面的 NEW 是什么呢?
------解决方案--------------------
--使用inserted CREATE TRIGGER UpDateStateTrigger ON commonauditorlog AFTER INSERT BEGIN IF inserted.AuditorRes > 0 UPDATE commonapplytable set State = State+1 where ApplyID = inserted.ApplyID and State != -1 ELSE UPDATE commonapplytable set State = -1 where ApplyID = inserted.ApplyID END go
------解决方案--------------------
CREATE TRIGGER UpDateStateTrigger ON commonauditorlog AFTER INSERT BEGIN IF exists(select 1 from inserted where AuditorRes > 0 ) UPDATE commonapplytable set State = a.State+1 from commonapplytable a inner jion inserted b on a.ApplyID = b.ApplyID where a.State != -1 ELSE UPDATE commonapplytable set State = -1 from commonapplytable a inner join inserted b on a.ApplyID = b.ApplyID END go
------解决方案--------------------
SQL Server 触发器不是逐行激发的,其只能由 insert 语句激发。
create trigger upDateStateTrigger on commonauditorlog for insert as update t set t.state=(case when l.auditorres>0 then t.state+1 else t.state=-1 end) from commonapplytable t inner join inserted l on t.applyid=l.applyid where t.state<>-1; go