我的触发器

CREATE TRIGGER procestate ON dbo.Instance
FOR INSERT
AS

  begin
 declare @Instance_id int
 select @Instance_id = Instance_id from inserted
 update Process
    set ProcessState=1
    where Process_Id=@Instance_id
 end



CREATE TRIGGER  Upate_Instance ON [dbo].[Instance]
FOR  UPDATE
AS begin  
             declare @instancestate int,@instanceid int,@instancepeople int,
                          @peopleshouquan int,@instancenote varchar(2500) ,@instancerole int,@grantflag bit
 select   @instanceid = Instance_id, @instancestate  = Instance_state,
                         @instancepeople = Instance_user, @instancenote = Instance_Note ,@instancerole=Instance_Role from inserted  
                          if @instancestate = 1 --更改状态
               begin
                            insert Have_done_Task select Instance_id,Instance_user,Instance_Note from inserted --插入记录到任务完成表中
  select @peopleshouquan = Grant_Id ,@grantflag = Grant_Flag from Staff_In_Role a where a.Staff_Id = @instancepeople and a.Role_Id=@instancerole --转到下一个人员   
                            if @grantflag=0 --流转到最后一位
                               update Process set ProcessState = 2 where Process_Id = @instanceid  
                            else
                                update Instance set Instance_user = @peopleshouquan,Instance_state=-1 where Instance_user = @instancepeople --转到下一个人员      
                       
end end  


 

你可能感兴趣的:(触发器)