定义游标(cursor)及使用步骤:
declare cursor_name cursor
--1.声明游标
[local(局部) | global(全局)]
[...其他请查阅] --默认是动态只读游标
for
select 语句
open cursor_name --2.打开游标
fetch next from cursor_name [into @变量1,@变量2.....]
--3.提取下一条数据
if(@@fetch_status = 0) --判断是否读取成功
begin
--0
表示行已成功读取
end
else if(@@fetch_status = -1)
begin
-- -1
表示读取操作已超出了结果集
end
else if(@@fetch_status = -2)
begin
-- -2
表示行在表中不存在
end
close [global] cursor_name
--4.关闭游标(全局游标需手动关闭)
deallocate [global] cursor_namr
--5.释放(删除)游标
SQLServer定义游标及使用,布布扣,bubuko.com
原文:http://www.cnblogs.com/tsyblog/p/3596646.html