数据库笔记五——游标

游标是从一种从表中检索并进行操作的工具

一、游标的使用:

 1、声明:

语法 
    decalare cursor_name [insertitive][scroll] cursor
      for select_statement
      for{read only|update[of column_name[,...n]]}]

示例:创建一个名为“MyCursor”的标准游标

use 销售管理系统
declare MyCursor cursor for
select * from 操作员信息表
go


创建只读游标

use 销售管理系统
declare MyCursor_01 cursor for
select *from 操作员信息表
for read only   —— 只读游标
go


2、打开

语法:
    open {{[global]cursor_name}|cursor_variable_name
      


global指定为全局游标

示例:

use 销售管理系统
declare MyCursor_001 cursor for —— 声明游标
select 操作员编号,操作员姓名,操作员年龄  from 操作员信息表
where 操作员标号='CY2004'
open MyCursor_001 ——打开游标
go


3、从游标中读取数据

语法:
fetch
    [[next|prior|first|las|absolute{n|@nvar}|relative{n|@nvar}]
     from
    ]
{{[global]cursor_name}|@cursorr_variable_name}
[into @variable_name[,...n]]

@@fetch_status:返回上次执行fetch命令的状态。返回值为0,fetch语句成功;为-1,fetch语句失败或此行不在结果中;为-2,被提取的行不存在

示例:

use 销售管理系统
declare readcursor cursor for 
select 操作员编号,操作员姓名,操作员性别,操作员住址
from 操作员信息表
open readcursor
fetch next from readcursor  ——执行取数操作
while @@fetch_status=0   ——检查,确定是否可以继续
begin
    fetch next from from readcursor
end


4、关闭

语法:close {{global]cursor_name}|cursor_variable_name}


示例:

use 销售管理系统
declare closecursor cursor for
select *from 销售表
for read only 
open closecursor  ——打开游标
close closecursor   ——关闭游标

5、释放

语法:deallocate {{[global]cursor_name}|@cursor_variable_name}

示例:

use 销售管理系统
declare freecursor cursor for
select * from 销售表
open freecursor
close freecursor
deallocate freecursor



 

二、游标的相关操作

1、删除数据

delete table_name
set column_name
where current of cursor_name

示例:

use 销售管理系统
declare deletecursor cursor
for select *from 操作员信息表
where 操作员编号=‘CY2006’
open deletecursor
go
fetch next from deletecursor
delete 操作员信息奥
where current of deletecursor  ——删除指定条件的数据
fetch next from deletecursor
go
close deletecursor
deallocate deletecursor
go

2、修改数据

示例:

declare @id char(20)
declare @ids char(20)
declare @name char(20)
set @id='CY2006'
declare authors_cursor cursor
     for select 操作员编号,造作员姓名 from 操作员信息表
open anthors_cursor
fetch next from authors_cursor
into  @ids,@names
while @@fetch_status=0
begin
     if @id=@ids
     begin
           update 操作员信息表 set 操作员年龄=30 where 操作员编号=@ids
    end
fetch next from anthors_cursor
into @ids,@names
end
close anthors_cursor
deallocate anthors_cursor

3、排序

示例:

use 销售管理系统
declare mycursor cursor
for select 商品编号,商品名称,库存数量,库存金额
from 库存表
order by  库存数量 DESC
open mycursor
fetch next from mycursor
while @@fetch_status=0
   fetch next form mycursor
close mucursor
deallocate mycursor


4、包含计算

示例:

declare cur cursor
for select 商品编号,商品名称,数量,金额,数量*金额 as 销售总额
from 销售表
open cur 库
fetch next from cur
while @@fetch_status=0
begin
   fetch next from cur
end 
close cur
deallocate cur

三、游标锁定

use pubs
set transaction isolation leval repeatable read  ——设置隔离级别为可重复
go
begin transaction
select * from authors
go


四、存储过程

sp_cursor_list  :报告当前为连接打开的服务器游标的特性

sp_describle_cursor:报告服务器游标的特性

sp_describe_cursor_lolumn:报告服务器游标结果集中的特性

sp_describe_cursor_table:报告服务器游标所引用的基表




 

 


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值