SQL Sever 教案第14章 通过游标提取和维护数据_sqlsever日常维护
SQL Sever 教案第14章 通过游标提取和维护数据由刀豆文库小编整理,希望给你工作、学习、生活带来方便,猜你可能喜欢“sqlsever日常维护”。
游标的作用是对查询的结果集能够执行逐行提取,普通的遍历虽然可以实现,但是这种类似于循环的遍历在结果集中的每一行都执行相同得操作的情况下使用起来很方便,但是如果每一行使用的操作都不相同的化,就不能够使用遍历这种循环的方式,因此游标营运而生,游标可以实现将结果集中的每一行记录提取并进行不同得操作。
游标的特点:
能够从多条记录的结果集中每次提取一条记录
游标由结果集和结果集中指向特定记录的游标位置组成游标的种类:
T-SQL游标,由declare cursor语法定义,主要用在T-SQL脚本、存储过程和触发器中、用在服务器上,不支持提取数据块或多行数据
API游标,用在服务器上,支持OLE DB,ODBC及DB_library 客户游标,当在客户机上缓存结果集时使用
应用游标的过程: 1)declare—声明游标
游标名称、数据来源(表和列)、选取条件、属性(仅读或可修改)2)open—打开游标
3)从一个游标中fetch(提取)信息 4)close或deallocate游标
--定义游标 use pubs go declare cur_roysched cursor for select pub_id,price from titles where price=19.99
游标的种类:
标准游标
--标准游标 use pubs go declare cur_authors cursor for select au_id, au_lname, au_fname, phone, addre, city, state, contract from authors
只读游标
--只读游标 use pubs go declare cur_authorsReade cursor for select au_lname, au_fname, phone, addre, city, state from authors for read only
更新游标
--更新游标 use pubs go declare cur_authorsUpdate cursor for select au_lname, au_fname from authors for update GO
游标在声明之后,如果要使用游标读取数据的化,必须要打开游标,--打开游标
open cur_roysched
当游标被打开之后,@@cursor_rows全局变量将用来记录游标内数据行数,其返回值有以下四种:-m:表示从基础表向游标读入数据的处理仍在进行,m表示当前的游标中的数据行数
-1:表示该游标是一个动态游标。由于动态游标反映基础表的所有变化,因此符合游标定义的数据行经常变动,故无法确定 0:表示无符合条件的记录或游标已被关闭
n:表示从基础表读入数据已经结束,n即为游标中已有数据记录的行数据
读取游标
--读取游标
fetch cur_roysched
读取游标过程中@@fetch_status全局变量返回上次执行fetch命令的状态,其有三个不同的返回值: 0:FETCH命令被成功执行
-1:FETCH命令失败或行数据超过游标数据结果集的范围-2:所读取的数据已经不存在在游标使用结束后要关闭游标来释放数据结果集和定位于数据记录上的锁--关闭游标
close cur_roysched
使用close命令关闭游标,但不释放游标占用的数据结构,如果准备在随后的使用中再次打卡游标,则最好使用close命令
另外如果在声明游标与释放游标之间使用了事务结构,则在结束事务时游标会自动关闭 如:
声明游标、打开游标 读取游标
begin transaction 数据处理
commit transaction--游标自动关闭
使用deallocate命令释放游标,它释放游标占用的数据结构,删掉游标与游标名或游标变量之间的联系,并释放游标占用的所有系统资源
游标变量:
将定义好的游标赋值给游标变量,然后再使用游标变量
--游标变量
--先声明一个游标 use pubs go declare titleauthor_cur scroll cursor for select * from titleauthor Go--使用set语句将游标赋值给变量 declare @cur_ta cursor set @cur_ta = titleauthor_cur--------或者以下列的方式进行赋值--或者将定义游标的语句放在赋值语句中 declare @cur_ta cursor declare titleauthor_cur scroll cursor for select * from titleauthor set @cur_ta = titleauthor_cur
附加:可以声明一种全局游标可以在批处理以外继续使用 use pubs go /*声明并打开一个全局游标在批处理以外该游标仍然可见*/ declare titleauthor_cursor cursor global scroll for select * from titleauthor open titleauthor_cursor go
/*用游标变量引用已声明过的游标*/ declare @cur_ta1 cursor set @cur_ta1 = titleauthor_cursor /*现在释放对游标的引用*/ deallocate @cur_ta1 /*游标titleauthor_cur 仍旧存在*/.fetch next from titleauthor_cursor Go
游标的定位和删除
--游标的定位和删除--先定义一个游标 use pubs go declare authors_cur scroll cursor for select * from authors for update of au_lname,au_fname go--更新authors表中的au_lname和au_fname列 update authors set au_lname = ‘china’, au_fname = ‘asia’ where current of authors_cur--删除authors表中的一行数据 delete from authors where current of authors_cu
创建一个存储过程,其中使用游标
--创建一个含有游标的存储过程
create proc pp @argcur cursor varying output as open @argcur fetch next from @argcur while @@fetch_status=0 begin fetch next from @argcur end deallocate @argcur go
declare titles_cur cursor for select * from titles go--deallocate titles_cur--定义一个游标变量,将游标赋值给游标变量 declare @oo cursor set @oo=titles_cur exec pp @oo--调用存储过程
嵌套游标的使用(难度稍大)
--嵌套游标
/* 创建存储过程*/ create procedure nestedcursor @au_id id as set nocount on /* 声明外部游标*/ declare outcursor cursor for select au_id,au_lname,au_fname from authors where au_id like @au_id
/* 声明内部游标*/ declare incursor cursor for select au_id, title_id, au_ord, royaltyper from titleauthor where au_id like @au_id declare @aau_id id, @au_lname varchar(40),@au_fname varchar(20)declare @tau_id id, @title_id tid, @au_ord tinyint, @royaltyper int /* 打开外部游标*/ open outcursor fetch next from outcursor into @aau_id, @au_lname, @au_fname print '********************************************************' print 'Information about the author:' select 'au_id'=@aau_id, 'au_lname'= @au_lname,'au_fname'= @au_fname while @@fetch_status =0 begin /* 打开内部游标*/ open incursor fetch next from incursor into @tau_id,@title_id, @au_ord, @royaltyper while @@fetch_status = 0 begin
if @tau_id=@aau_id
begin
select 'title_id'=@title_id, 'au_ord'=@au_ord, 'royaltyper'=@royaltyper
end
fetch next from incursor into @tau_id,@title_id,@au_ord,@royaltyper end close incursor fetch next from outcursor into @aau_id,@au_lname,@au_fname print
'**********************************************************' print 'Information about the author' select
'au_id'=@aau_id,'au_lname'=@au_lname,'au_fname'=@au_fname end deallocate incursor close outcursor deallocate outcursor
游标的作用是对查询的结果集能够执行逐行提取,普通的遍历虽然可以实现,但是这种类似于循环的遍历在结果集中的每一行都执行相同得操作的情况下使用起来很方便,但是如果每一行使......
刀豆文库小编为你整合推荐5篇数据采编录入和维护合同,也许这些就是您需要的文章,但愿刀豆文库能带给您一些学习、工作上的帮助。......
数据采编录入和维护合同范本在人们的法律意识不断增强的社会,人们运用到合同的场合不断增多,它也是实现专业化合作的纽带。拟定合同的注意事项有许多,你确定会写吗?以下是小编为......
需方:_________供方:_________一、定义1.1 定义:本合同中的定义适用于本合同的全部条款及本合同的全部附件以及需方招标文件和供方投标文件。1.1.1 合同:载明需方和供方就本次招......
1.打开ArcGISC\Users\Administrator\Documents\ArcGIS\中国分省矢量文件地理数据库;C\Users\Administrator\Documents\ArcGIS\中国气象数据地理数据库(MetStation_V......
