SQL中游标(cursor)的基本使用实例
类型:
1.普通游标 只有next操作
2.滚动游标 有多种操作
1.普通游标
declare @username varchar(20),@userid varchar(100) declare cursor_name cursor for --定义游标 select top 10 userid,username from userinfo order by userid desc open cursor_name --打开游标 fetch next from cursor_name into @userid,@username --抓取下一行游标数据 while @@fetch_status = 0 begin print '用户id:'+@userid+' '+'用户名:'+@username fetch next from cursor_name into @userid,@username end close cursor_name --关闭游标 deallocate cursor_name --释放游标
结果:
用户id:zhizhi 用户名:邓鸿芝
用户id:yuyu 用户名:魏雨
用户id:yujie 用户名:李玉杰
用户id:yuanyuan 用户名:王梦缘
用户id:youyou 用户名:lisi
用户id:yiyiren 用户名:任毅
用户id:yanbo 用户名:王艳波
用户id:xuxu 用户名:陈佳绪
用户id:xiangxiang 用户名:李庆祥
用户id:wenwen 用户名:魏文文
2.滚动游标
--带scroll选项的游标 set nocount on declare c scroll cursor for --scorll 后,有了更多的游标操作(滚动游标) select top 10 userid,username from userinfo order by userid desc open c fetch last from c --最后一行的数据,并将当前行为指定行 fetch absolute 4 from c --从第一行开始的第4行数据,并将当前行为指定行 这里的n可正可负,n>0 往下翻,n<0 往上翻 fetch relative 3 from c --相对于当前行的后3行数据,并将当前行为指定行 这里的n可正可负 fetch relative -2 from c --相对于当前行的前2行数据,并将当前行为指定行 fetch prior from c ----相对于当前行的前1行数据 fetch first from c --刚开始第一行的数据,并将当前行为指定行 fetch next from c --相对于当前行的后1行数据 close c deallocate c
结果(可以参考第一个结果分析):
具体fetch用法:
fetch [ [ next | prior | first | last | absolute { n | @nvar } | relative { n | @nvar } ] from ] { { [ global ] cursor_name } | @cursor_variable_name } [ into @variable_name [ ,...n ] ]
arguments
next
returns the result row immediately following the current row and increments the current row to the row returned. if fetch next is the first fetch against a cursor, it returns the first row in the result set. next is the default cursor fetch option.
prior
returns the result row immediately preceding the current row, and decrements the current row to the row returned. if fetch prior is the first fetch against a cursor, no row is returned and the cursor is left positioned before the first row.
first
returns the first row in the cursor and makes it the current row.
last
returns the last row in the cursor and makes it the current row.
absolute { n| @nvar}
if n or @nvar is positive, returns the row n rows from the front of the cursor and makes the returned row the new current row. if n or @nvar is negative, returns the row n rows before the end of the cursor and makes the returned row the new current row. if n or @nvar is 0, no rows are returned. n must be an integer constant and @nvar must be smallint, tinyint, or int.
relative { n| @nvar}
if n or @nvar is positive, returns the row n rows beyond the current row and makes the returned row the new current row. if n or @nvar is negative, returns the row n rows prior to the current row and makes the returned row the new current row. if n or @nvar is 0, returns the current row. if fetch relative is specified with n or @nvar set to negative numbers or 0 on the first fetch done against a cursor, no rows are returned. n must be an integer constant and @nvar must be smallint, tinyint, or int.
global
specifies that cursor_name refers to a global cursor.
cursor_name
is the name of the open cursor from which the fetch should be made. if both a global and a local cursor exist with cursor_name as their name, cursor_name to the global cursor if global is specified and to the local cursor if global is not specified.
@cursor_variable_name
is the name of a cursor variable referencing the open cursor from which the fetch should be made.
into @variable_name[ ,...n]
allows data from the columns of a fetch to be placed into local variables. each variable in the list, from left to right, is associated with the corresponding column in the cursor result set. the data type of each variable must either match or be a supported implicit conversion of the data type of the corresponding result set column. the number of variables must match the number of columns in the cursor select list.
总结
到此这篇关于sql中游标(cursor)基本使用的文章就介绍到这了,更多相关sql游标的使用内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: Redis的六种底层数据结构(小结)