首页 > 代码库 > 存储过程 游标

存储过程 游标

CREATE  PROCEDURE [dbo].[RuleExecute_Temp]ASdeclare @executedate varchar(20)declare @branchcode varchar(50)declare @rulenumber varchar(50)declare @number varchar(20)declare @JLWZCode varchar(20)declare cur cursor for SELECT ExcuteDate,RuleNumber,BranchCode,number,JLWZCode FROM IM_ReRun where Flag=0open curfetch cur into @executedate,@rulenumber,@branchcode,@number,@JLWZCodewhile (@@Fetch_Status=0)begin    exec RuleExecute @rulenumber,@executedate,1,@branchcode         declare cur1 INSENSITIVE SCROLL cursor for select result,occurdate from  dbo.IM_Result where number=@number and BranchCode=@branchcode and seriesID=0 and executedate=@executedate  and rulenumber=@rulenumber        declare @result varchar(20)        declare @occurdate varchar(20)         open cur1         fetch first from cur1 into @result,@occurdate    if(@result=正常)    begin      declare cur2 INSENSITIVE SCROLL cursor for select ZBcode,ZDcode,chancode,Type,TableSource from dbo.IM_DataSource where number=@number      declare @ZBcode varchar(20)      declare @ZDcode varchar(20)      declare @chanCode varchar(20)      declare @type varchar(20)      declare @TableSource varchar(100)      open cur2      fetch first from cur2 into @ZBcode,@ZDcode,@chanCode,@type,@TableSource      declare @datet varchar(20) set @datet=DateYMD      if(@type=M)      begin       set @datet=DateYM       set @occurdate=substring(@occurdate,0,7)      end       declare @sql varchar(1000)       set @sql=update +@TableSource+ set flag=0 where zbcode=+@ZBcode+ and +@datet+=+@occurdate+ and BranchCode=+@JLWZCode+‘‘       exec(@sql)      close cur2      Deallocate cur2    end    close cur1    Deallocate cur1    update IM_ReRun set Flag=1 where RuleNumber=@rulenumber and BranchCode=@branchcode and ExcuteDate=@executedate    fetch next from cur into @executedate,@rulenumber,@branchcode,@number,@JLWZCodeendclose curDeallocate curGO

该存储过程游标内又定义了两个游标cur1和cur2。

并且这两个游标只取select结果的第一行数据。

所以在定义时需要加上“INSENSITIVE SCROLL” scroll表示可随意移动游标指 针(否则只能向前),dynamic表示可以读写游标(否则游标只读)*/  ,不然会报错,错误会提示fetch: 提取类型first不能与只进游标一起使用。

存储过程 游标