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
作者:造梦者2013
出处:http://www.cnblogs.com/lipanpan/
本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

浙公网安备 33010602011771号