sql关于游标存储过程编写笔记

原理:游标就是把数据按照指定要求提取出相应的数据集,然后逐条进行数据处理。

相关说明:

@@FETCH_STATUS
返回被 FETCH 语句执行的最后游标的状态,而不是任何当前被连接打开的游标的状态。 
返回值 描述 
0 FETCH 语句成功。 
-1 FETCH 语句失败或此行不在结果集中。 
-2 被提取的行不存在。
最简单游标声明:DECLARE <游标名>CURSOR FOR<SELECT语句>;
create  procedure [dbo].[Proc_Test]
as
begin

     declare @id int,@mode int, @tempRec varchar(512);
     declare cur_index cursor for     
     select Id,AuditMode,StepPosition
     from Rfa_Tb_WFWorkFlowStep where FlowId='1006';     
     
     open cur_index 
     fetch next from cur_Index into @id,@mode,@tempRec
     while @@FETCH_STATUS=0
     begin 
      print 'Id:'+convert(varchar(10),@id,21)+'AuditMode:'+convert(varchar(10),@mode,21)+'StepPosition:'+convert(varchar(10),@tempRec,21); 
      
      fetch next from cur_Index into @id,@mode,@tempRec
     end
     print 'success'
     close cur_index
     deallocate cur_index      
end

 

       
posted @ 2014-07-15 18:13  造梦者2013  阅读(155)  评论(0)    收藏  举报