定义游标的基本语法:(效率低)
1.declare 游标名 cursor [local|global][游标选项] for 查询语句
2.open 游标名
3.fetch [next] from 游标名 into @v1,@v2..
4.判断@@fetch_status状态,继续fetch
5.close 游标名 -------关闭游标
6.deallocate 游标名--删除游标中数据库中保存的数据结构(彻底释放资源)
游标示例代码:
declare cur_MyStudent cursor fast_forward for select * from tblStudent
open cur_MyStudent
fetch next from cur_MyStudent--像循环一样往下一条一条地走
while @@fetch_status=0
begin
fetch next from cur_MyStudent
end
close cur_MyStudent
deallocate cur_MyStudent