SQL Server 2008 游标
能使用集合的就尽量不使用游标,因为效率低,而且步骤多维护难
定义简单变量,只能存放一个值,是个标量
declare @name nvarchar(100) select @name = name from student print @name; --如果没有循环的话,只会打印出最后一行的字段值
1、声明游标
declare c cursor for select id, name from student; declare @sname nvarchar(100); declare @sid int;
2、打开游标
open c;
3、从游标中读取查询的数据
fetch next from c into @sid, @sname;
4、循环判断获取的数据是否符合实际的数据
--4、注意fetch并不一定真能拿到实际的数据 -----可以靠循环判断所有获取的数据是否符合 while @@FETCH_STATUS = 0 --成功获取了数据 begin print @sname; --然后,试探获取下一条数据 fetch next from c into @sid, @sname; end
5、游标使用完毕,一定要关闭
close c;
6、释放游标
deallocate c;
                    
                
                
            
        
浙公网安备 33010602011771号