比较简单的一个触发器,可一直报“缺少BEFORE,AFTER,INSTEAD OF 关键字”,求高手帮忙看看错在哪里?谢谢大家了。
两表基本数据:
tb_stage:(stageid,statevalueid,projectid)
tb_project:(projectid,projectstateid)
一个项目下有可有多个阶段,当所属项目下的阶段的状态被更新后(阶段状态可手动更新),则检查该项目下的阶段状态是否全部完成,若是,则置项目状态为完成。触发器代码如下:
其中5代表完成,6代表已删除
create trigger test
on tb_stage
after update
as 
declare @pid integer
declare @count_one integer
declare @count_two integer
select @pid=projectid from inserted
select @count_one=count(*) from tb_stage where statevalueid=5 and projectid=@pid
select @count_two=count(*) from tb_stage where statevalueid!=6 and projectid=@pid
if(@count_one==@count_two)
begin
update tb_project set projectstateid=2 where projectid=@pid
end

解决方案 »

  1.   

    create or replace trigger test
      after update on tb_stage  
      as
      declare @pid integer;
      declare @count_one integer
      declare @count_two integer
      select @pid=projectid from inserted
      select @count_one=count(*) from tb_stage where statevalueid=5 and projectid=@pid
      select @count_two=count(*) from tb_stage where statevalueid!=6 and projectid=@pid
      if(@count_one==@count_two)
    begin
      update tb_project set projectstateid=2 where projectid=@pid
    end test;
    不好意思,上面的触发器建错地方了。现在触发器如上,但却在声明变量的时候又出错了,到底怎么写这个触发器了??求高手出山帮忙!!本人不甚感激