广告
返回顶部
首页 > 资讯 > 后端开发 > 其他教程 >SQL中游标(cursor)的基本使用实例
  • 180
分享到

SQL中游标(cursor)的基本使用实例

2024-04-02 19:04:59 180人浏览 泡泡鱼
摘要

目录 类型:1.普通游标2.滚动游标具体FETCH用法:Arguments总结 类型:   1.普通游标   只有NEXT操作   2.滚动游标 有多种操作 1.普通游标 DEC

 类型:

  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游标的使用内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: SQL中游标(cursor)的基本使用实例

本文链接: https://www.lsjlt.com/news/158108.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • SQL中游标(cursor)的基本使用实例
    目录 类型:1.普通游标2.滚动游标具体FETCH用法:Arguments总结 类型:   1.普通游标   只有NEXT操作   2.滚动游标 有多种操作 1.普通游标 DEC...
    99+
    2022-11-12
  • SQL中cursor的基本使用方法是什么
    这篇文章主要介绍“SQL中cursor的基本使用方法是什么”,在日常操作中,相信很多人在SQL中cursor的基本使用方法是什么问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”SQL中cursor的基本使用方法...
    99+
    2023-06-25
  • SQL中游标的基本使用方法有哪些
    在SQL中,游标的基本使用方法包括以下几步:1. 声明游标:使用DECLARE语句来声明一个游标,并指定游标的名称、游标的数据类型以...
    99+
    2023-08-12
    SQL
  • Oracle中的游标Cursor怎么使用
    在Oracle中,游标(Cursor)是一种用于处理结果集的数据库对象。游标可以被用来遍历和操作查询结果集中的行。以下是使用游标的基...
    99+
    2023-08-15
    Oracle Cursor
  • sql中exists的基本用法示例
    目录【exists语句的执行顺序如下】:附:exists与in比较总结:现有:班级表(A_CLASS) 学生表( STUDENT) 注:学生表(STUDENT)的classId关联班级表(A_CLASS)的主键ID ...
    99+
    2022-08-16
    sql exists语句 sql中的exists的作用 exists查询语句
  • sybase中游标的使用示例
    以下是在sybase使用游标的示例,因为当初使用这个示例消耗了一定的时间,先特做以记录 --查询直接授予用户的对象权限 DECLARE cur_DBName CURS...
    99+
    2022-10-18
  • egg.js的基本使用实例
    目录安装egg.js写第一个api接口创建控制器编写路由关闭csrf开启跨域数据库配置和创建迁移文件创建数据迁移表模型创建模型错误和异常处理中间件参数验证安装egg.js 全局切换镜...
    99+
    2022-11-13
  • SQL PLUS基本命令的使用方法示例
    1、Oracle 中 dba,all,user, 之间的区别 1. 结论:'权限大小不同': dba_* > all_* > user_* (1) dba_* ...
    99+
    2022-11-12
  • Java NIO的基本使用实例
    这篇文章主要介绍“Java NIO的基本使用实例”,在日常操作中,相信很多人在Java NIO的基本使用实例问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Java NIO的基本使用实例”的疑惑有所帮助!接下来...
    99+
    2023-06-17
  • SQL Server中的游标怎么定义和使用
    这篇文章主要介绍“SQL Server中的游标怎么定义和使用”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“SQL Server中的游标怎么定义和使用”文章能帮助大家解决问题。游标...
    99+
    2023-06-30
  • Vue3中vuex的基本使用方法实例
    目录一、基本结构二、基本使用三、将store中的数据模块化后的使用1.模块化2.使用补充:如何改变vuex中的属性总结 一、基本结构 src/store/index.js中...
    99+
    2022-11-13
  • ES6中常见基本知识点的基本使用实例汇总
    目录前言1、字面量的增强2、解构Destructuring3、let和const4、作用域5、模板子符串6、函数7、展开语法8、数值表示9、Symbol的基本使用10、Set11、W...
    99+
    2022-11-13
  • JavaScript中Set基本使用方法实例
    目录介绍基本API1. 创建Set实例2. Set实例转数组3. size属性4. add()5. has()6. delete()7. clear()8. 迭代补充:JS...
    99+
    2022-11-16
    js set使用 js中set用法 javascript set
  • MySql存储过程和游标的使用实例
    目录前言1.创建存储过程。2.查看存储过程名称3.调用存储过程4.删除存储过程总结前言 这里存储过程和游标的定义和作用就不介绍了,网上挺多的,只通过简单的介绍,然后用个案例让大家快速...
    99+
    2022-11-13
  • sass在react中的基本使用(实例详解)
    目录1. 安装sass2. 编写App.tsx中的基本DOM3. sass变量4. sass中的选择器嵌套和属性嵌套5. sass中的@import和Partials6. Sass中...
    99+
    2022-11-13
  • Vue中transition标签的基本使用教程
    目录transition 标签配合 animation配合 transitiontransition-group 标签动画库 animate.css总结transition 标签 t...
    99+
    2022-11-13
  • vue3.x中emits的基本用法实例
    这是官方的文字介绍。emits重要用于组件之间的通信,触发自定义事件,传递参数。 下面演示一个子组件把事件传递到父组件,组件间通信的例子。 <template> ...
    99+
    2022-11-13
  • vue具名插槽的基本使用实例
    前言 具有名字的插槽slot使用 中的 "name" 属性绑定元素 注意: 1,如果没有匹配到 则放到匿名的插槽中 2,具名插槽的渲染顺序,完全取决于模板,而不是取决于父组件中...
    99+
    2022-11-12
  • MySQL中JOIN连接的基本用法实例
    目录join流程详解一、笛卡尔积:CROSS JOIN二、内连接:INNER JOIN三、左连接:LEFT JOIN四、右连接:RIGHT JOIN五、全连接:O...
    99+
    2022-11-13
  • JavaWeb之Ajax的基本使用与实战案例
    目录一、Ajax是什么?二、为什么使用Ajax?三、Ajax基本使用1、$.ajax()2、$.post() 3.$.get() 四、案例无刷新登录(ajax、g...
    99+
    2022-11-13
    java web ajax javaweb ajax使用 Ajax的使用
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作