游标 的使用

游标允许应用程序对查询语句select 返回的行结果集中每一行进行相同或不同的操作,而不是一次对整个结果集进行同一种操作;

它还提供对基于游标位置而对表中数据进行删除或更新的能力

但是 ,处理大数据量时,效率低下,占用内存大 ;能不用尽量不用

例子:

 declare @id varchar(50)
 declare @name varchar(50)
 declare cursor1 cursor for         --定义游标cursor1
 select id,doccode  from aa where id<3       --使用游标的对象(跟据需要填入select文)
  open cursor1                      --打开游标

 fetch next from cursor1 into @id,@name  --将游标向下移1行,获取的数据放入之前定义的变量@id,@name中
 
while @@fetch_status=0           --判断是否成功获取数据
begin
    select  @id , @name
    fetch next from cursor1 into @id,@name  --将游标向下移1行
end
select   @id + @name
close cursor1                   --关闭游标
deallocate cursor1                --释放游标
原文地址:https://www.cnblogs.com/cl1006/p/6372730.html