定位到结果集中某一行
游标分类:
(1)静态游标(static):在操作游标的时候,数据发生变化,游标中的数据不变
(2)动态游标(Dynamic):在操作游标的时候,数据发生变化,游标中的数据改变,默认值
(3)键集驱动游标(keyset):在操作游标的时候,被标识的列发生改变,游标中的数据改变,其他列改变,游标中的数据不变
语法:
创建游标:--scroll:游标 没有scroll:只进
declare 游标名 cursor scroll
for 查询语句
例:
declare mycur cursor scroll
for select menber1 from member
打开游标
open 游标名
关闭游标
close 游标名
删除游标
deallocate 游标名
提取某行数据
fetch first from 游标名---第一行
fetch cast from 游标名---最后一行
fetch absulute 2 from 游标名---提取第二行
fetch relatice 2from 游标名---当前行下移2行
fetch next from 游标名---下移1行
fetch prior from 游标名---上移一行
例:创建指向某行多列的游标,循环显示多列数据
derlare 游标名 cursor scroll
for select 字段1,字段2,字段3 from 表名
open 游标名
declare @a varchar(20)
declare @b varchar(20)
declare @c varchar(20)
fetch absolute 1 from 游标名 into @a,@b,@c where @@fetch_status=0
begin
print '用户名'+@a+'密码'+@b
fetch next from 游标 into @a,@b,@c
end