且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

SQL Server 2005系列教学(13) 游标

更新时间:2022-09-01 14:26:34

定义一个标准游标:
declare mycursor cursor for select * from yuangong 
定义一个只读游标:
declare mycursor cursor for select * from yuangong for read only
定义一个可写游标:
declare mycursor1 cursor    for select * from yuangong for update of
 姓名,性别,年龄,基本工资,奖金,所得税,应发工资
注: scroll只能只读游标起作用

打开游标:
open 游标名
如:
declare mycursor cursor for select  * from yuangong
open  mycursor
从游标中取数据:
fetch
默认情况下,指针指向第一条记录之前
移动记录指针的方法:
NEXT   下移一条记录
prior  上移一条记录
first  第一条记录
LAST   最后一条记录
absolute n  绝对记录 第N条记录
取数据语法:
fetch next|prior|first|last|absolute n    from 游标名  [into 变量名列表]

关闭游标: close 游标名
暂时关闭游标,还可再使用OPEN打开.
释放游标: deallocate 游标名
从内存中清除游标.如果还想使用,必须再次声明.
对当前游标状态进行判断:
@@fetch_status  如果返回是0,说明当前操作是成功的.否则是失败的.
0 FETCH 语句成功。 
-1 FETCH 语句失败或此行不在结果集中。 
-2 被提取的行不存在。

利用游标从员工表取出所有数据:
declare @i int
declare mycursor cursor 
for select * from yuangong
open mycursor
select @i=count(*) from yuangong
 fetch next from mycursor 
while @@fetch_status=0 and @i>1
begin
 fetch next from mycursor 
 set @i=@i-1
end
close mycursor
deallocate mycursor
 
作业  高考批改系统:
 姓名  性别  分数
 刘满   男    80
 佟亮   男    70
 张三   女    80
 李逍遥  男   50
 赵灵儿  女  90

小于60 不及格
60---70 及格
70--80  良好
80--100  优秀

刘满同学:优秀!!!
佟亮同学:良好!!
...
 
eclare @username varchar(10),@score int
declare my_cur cursor for select 姓名,分数 from student 
open my_cur
print space(27)+'2007年高考批改系统'
fetch next from my_cur into @username,@score
while @@fetch_status=0
begin
     
    if @score<60
    begin
        print  space(30)+@username+':不及格'
    end 
    else
    begin
           if @score >=60 and @score <70
           begin
               print space(30)+@username+':及格'
           end
           else
           begin
               if @score>=70 and @score<80
               begin
                  print  space(30)+@username+':良好'
               end
               else
               begin
                  print  space(30)+@username+':优秀'
               end
           end
    end
  fetch next from my_cur  into @username,@score
end
close my_cur
deallocate my_cur

通过游标修改数据表的信息:
语法: update  表名 set 字段名1=值1,字段名=值2..... where current of 游标名
通过游标删除数据表的信息:
语法: delete from   表名 where current of 游标名
select  * from student

通过游标把王五的分数改为99

declare @username varchar(10),@score int
declare my_cur cursor for select 姓名,分数 from student for update of 姓名,分数
open my_cur
fetch next from my_cur into @username,@score 
while @@fetch_status=0
begin
     if @username='王五'
     begin 
         update student set 分数=99 where current of my_cur   
     end
     else
     begin
 fetch next from  my_cur into @username,@score 
     end
end
deallocate my_cur
*********************  上面的代码为什么是死循环?如何更改**********
 
通过游标从student表中删除 王五的记录
declare @username varchar(10)
declare my_cur cursor for select 姓名 from student for update 
open my_cur
fetch next from my_cur into @username 
while @@fetch_status=0
begin
     if @username='王五'
     begin 
        delete from student  where current of my_cur   
     end
     fetch next from  my_cur into @username
end
deallocate my_cur
 
 
 
 
 
 本文转自 dufei 51CTO博客,原文链接:http://blog.51cto.com/dufei/80764,如需转载请自行联系原作者