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

本文涉及的产品
RDS SQL Server Serverless,2-4RCU 50GB 3个月
推荐场景:
云数据库 RDS SQL Server,独享型 2核4GB
简介:
定义一个标准游标:
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,如需转载请自行联系原作者

相关实践学习
SQL Server on Linux入门教程
SQL Server数据库一直只提供Windows下的版本。2016年微软宣布推出可运行在Linux系统下的SQL Server数据库,该版本目前还是早期预览版本。本课程主要介绍SQLServer On Linux的基本知识。 相关的阿里云产品:云数据库RDS&nbsp;SQL Server版 RDS SQL Server不仅拥有高可用架构和任意时间点的数据恢复功能,强力支撑各种企业应用,同时也包含了微软的License费用,减少额外支出。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/sqlserver
相关文章
|
19天前
|
SQL IDE Java
Java连接SQL Server数据库的详细操作流程
Java连接SQL Server数据库的详细操作流程
|
3天前
|
SQL 存储 关系型数据库
关系型数据库SQL Server学习
【7月更文挑战第4天】
11 2
|
6天前
|
SQL Java BI
SQL游标的使用方法
SQL游标的使用方法
|
7天前
|
SQL 存储 测试技术
|
6天前
|
SQL 机器学习/深度学习 搜索推荐
SQL SERVER 转换失败
【6月更文挑战第25天】
|
11天前
|
SQL 关系型数据库 分布式数据库
PolarDB产品使用问题之如何迁移SQL Server
PolarDB产品使用合集涵盖了从创建与管理、数据管理、性能优化与诊断、安全与合规到生态与集成、运维与支持等全方位的功能和服务,旨在帮助企业轻松构建高可用、高性能且易于管理的数据库环境,满足不同业务场景的需求。用户可以通过阿里云控制台、API、SDK等方式便捷地使用这些功能,实现数据库的高效运维与持续优化。
|
7天前
|
SQL 存储 Java
SQL游标的应用场景及使用方法
SQL游标的应用场景及使用方法
|
8天前
|
SQL Java 数据处理
SQL游标的详细使用方法与实例
SQL游标的详细使用方法与实例
|
10天前
|
SQL 存储 机器人
SQL Server 中 RAISERROR 的用法详解
SQL Server 中 RAISERROR 的用法详解
|
19天前
|
SQL IDE Java
Java连接SQL Server数据库的详细操作流程
Java连接SQL Server数据库的详细操作流程