1. /*
  2. 游标 cursor
  3. 什么是游标?为什么需要游标
  4. 使用存储过程对sql进行编程的时候,我们查询的语句可能是数据是多个,它总是一口气全部执行,我们无法针对每一条进行判断。也就是说,我们无法控制程序的运行,所以引入了游标cursor
  5. cursor类似于java中的迭代器。 它利用查询语句生成一个游标,然后游标中有一个类似指针的东西。首先指在游标首,就是迭代器。不解释了
  6.  
  7. cursor 游标
  8. declare声明; declare 游标名 cursor for select_statement;
  9. open 打开; open游标名
  10. fetch 取值; fetch 游标名 into var1,var2[,...] select语句中查出的项有多少,就需要使用多少变量接受
  11. close 关闭; close 游标名
  12. */
  13.  
  14. create table goods
  15. (
  16. id int,
  17. name varchar(20),
  18. num int
  19. );
  20. insert into goods values (1,'dog',20),(2,'cat',30),(3,'pig',25);
  21. select * from goods;
  22.  
  23. -- 游标在存储过程中使用
  24.  
  25. drop procedure p1;
  26. create procedure p1()
  27. begin
  28.  
  29. declare row_id int;
  30. declare row_name varchar(20);
  31. declare row_num int;
  32. declare gs cursor for select id,name,num from goods; -- 声明游标的语句后面不能有声明变量
  33.  
  34. open gs;
  35. fetch gs into row_id,row_name,row_num;
  36. select row_id,row_name,row_num;
  37. close gs;
  38.  
  39. end;
  40.  
  41. call p1();
  42.  
  43. create procedure p2()
  44. begin
  45.  
  46. declare row_id int;
  47. declare row_name varchar(20);
  48. declare row_num int;
  49. declare gs cursor for select id,name,num from goods; -- 声明游标的语句后面不能有声明变量
  50.  
  51. open gs;
  52. fetch gs into row_id,row_name,row_num;
  53. fetch gs into row_id,row_name,row_num;
  54. fetch gs into row_id,row_name,row_num;
  55. select row_id,row_name,row_num;
  56. close gs;
  57.  
  58. end;
  59.  
  60. call p2(); --报错,如果取出游标数据的个数超过游标中数据的个数,报错。类似于数组越界
  61.  
  62. drop procedure p3;
  63. create procedure p3()
  64. begin
  65.  
  66. declare row_id int;
  67. declare row_name varchar(20);
  68. declare row_num int;
  69. declare gs cursor for select id,name,num from goods; -- 声明游标的语句后面不能有声明变量
  70.  
  71. open gs;
  72. fetch gs into row_id,row_name,row_num;
  73. select row_id,row_name,row_num;
  74. fetch gs into row_id,row_name,row_num;
  75. select row_id,row_name,row_num;
  76. fetch gs into row_id,row_name,row_num;
  77. select row_id,row_name,row_num;
  78. close gs;
  79.  
  80. end;
  81. call p3();
  82.  
  83. --学会使用循环控制试试
  84. create procedure p4()
  85. begin
  86. declare row_id int;
  87. declare row_name varchar(20);
  88. declare row_num int;
  89. declare count_r int;
  90. declare i int default 0;
  91.  
  92. declare gs cursor for select id,name,num from goods; -- 游标声明语句好像位置有限定。不能在声明变量前面,不能再哎select语句后面
  93. select count(*) into count_r from goods;
  94.  
  95. open gs;
  96. repeat
  97. fetch gs into row_id,row_name,row_num;
  98. select row_id,row_name,row_num;
  99. set i := i+1;
  100. until i>=count_r end repeat;
  101. close gs;
  102. end;
  103.  
  104. call p4();
  105.  
  106. -- while循环试试
  107. create procedure p5()
  108. begin
  109. declare row_id int;
  110. declare row_name varchar(20);
  111. declare row_num int;
  112. declare count_r int;
  113. declare i int default 0;
  114.  
  115. declare gs cursor for select id,name,num from goods; -- 游标声明语句好像位置有限定。不能在声明变量前面,不能再哎select语句后面
  116. select count(*) into count_r from goods;
  117.  
  118. open gs;
  119. while i<count_r do
  120. fetch gs into row_id,row_name,row_num;
  121. select row_id,row_name,row_num;
  122. set i := i+1;
  123. end while;
  124. close gs;
  125. end;
  126.  
  127. call p5();
  128.  
  129. -- 使用游标最主要的是可以针对每一次查出来的结果进行一些操作
  130. drop procedure p6;
  131. create procedure p6()
  132. begin
  133. declare row_id int;
  134. declare row_name varchar(20);
  135. declare row_num int;
  136. declare count_r int;
  137. declare i int default 0;
  138.  
  139. declare gs cursor for select id,name,num from goods; -- 游标声明语句好像位置有限定。不能在声明变量前面,不能再哎select语句后面
  140. select count(*) into count_r from goods;
  141.  
  142. open gs;
  143. while i<count_r do
  144. fetch gs into row_id,row_name,row_num;
  145. if row_num>25 then select concat(row_name,'比较多');
  146. elseif row_num=25 then select concat(row_name,'刚刚好');
  147. else select concat(row_name,'有点少');
  148. end if;
  149. set i := i+1;
  150. end while;
  151. close gs;
  152. end;
  153.  
  154. call p6();
  155.  
  156. -- 第三种方式:游标越界时候使用标志,利用标识来结束
  157. -- mysql cursor中,可以使用declare continue handler来操作一个越界标识
  158. -- declare continue handler for not found statement
  159. drop procedure p7;
  160. create procedure p7()
  161. begin
  162. declare row_id int;
  163. declare row_name varchar(20);
  164. declare row_num int;
  165.  
  166. declare you int default 1;
  167. declare gs cursor for select id,name,num from goods;
  168. declare continue handler for not found set you:=0;
  169.  
  170. open gs;
  171. while you!=0 do
  172. fetch gs into row_id,row_name,row_num;
  173. if you!=0 then select row_num,row_name;
  174. end if;
  175. end while;
  176. close gs;
  177. end;
  178. call p7();

cursor游标(mysql)的更多相关文章

  1. 【PLSQL】变量声明,结构语句,cursor游标

    ************************************************************************   ****原文:blog.csdn.net/clar ...

  2. 转 oracle cursor 游标

    转自:http://blog.csdn.net/liyong199012/article/details/8948952 游标的概念:     游标是SQL的一个内存工作区,由系统或用户以变量的形式定 ...

  3. DRF url控制 解析器 响应器 版本控制 分页(常规分页,偏移分页,cursor游标分页)

    url控制 第二种写法(只要继承了ViewSetMixin) url(r'^pub/$',views.Pub.as_view({'get':'list','post':'create'})), #获取 ...

  4. mysql cursor游标的使用,实例

    mysql被oracle收购后,从mysql-5.5开始,将InnoDB作为默认存储引擎,是一次比较重大的突破.InnoDB作为支持事务的存储引擎,拥有相关的RDBMS特性:包括ACID事务支持,数据 ...

  5. 存储过程/游标/mysql 函数

    存储过程和函数(存储在 mysql数据库中的 proc表,所以检查有没有这个表)存储过程是一种存储程序(如正规语言里的子程序一样),mysql支持有两种:存储过程,在其他SQL语句中可以返回值的函数( ...

  6. SQL SERVER CURSOR游标的使用(转载)

    一:认识游标 游标(Cursor)它使用户可逐行访问由SQL Server返回的结果集. 使用游标(cursor)的一个主要的原因就是把集合操作转换成单个记录处理方式. 用SQL语言从数据库中检索数据 ...

  7. SQL Cursor 游标的使用

    DECLARE @name VARCHAR(50)  --声明游标 DECLARE cursor_VAA1 CURSOR FOR SELECT VAA05 FROM VAA1 --打开游标 OPEN ...

  8. SQL Cursor(游标)

    1.游标在数据表没有id(identity(1,1))时好用,但是游标会吃更多的内存,减少可用的并发,占用宽带,锁定资源,当然还有更多的代码量 2.如果能不用游标,尽量不要使用游标,用完用完之后一定要 ...

  9. 关键字(5):cursor游标:(循环操作批量数据)

    declare   cursor stus_cur is select * from students;  --定义游标并且赋值(is 不能和cursor分开使用)    cur_stu studen ...

随机推荐

  1. path.resolve()和path.join()

    resolve 作用:path.resolve() 该方法将一些的 路径/路径段 解析为绝对路径. 语法:path.resolve([...paths]) 说明: ...paths <strin ...

  2. Camera帧率和AE的关系

    1.camera首先是通过曝光的pixel加上dummy pixel以及曝光的line加上dummy line来决定一帧的曝光时间,这一帧曝光时间的倒数就是帧率,这个没有错吧,但是看代码时候看到pre ...

  3. python---自动群发邮件

    生活中我们经常发送邮件,那么我们能不能用Python写一个自动发送邮件的功能呢?答案是肯定的!!! 开始实现功能之前我们需要开启我们邮箱的 IMAP/SMTP功能,我们先了解一下什么是IMAP/SMT ...

  4. 照着官网来安装openstack pike之nova安装

    nova组件安装分为控制节点和计算节点,还是先从控制节点安装 1.前提条件,数据库为nova创建库和账户密码来连接数据库 # mysql -u root -p MariaDB [(none)]> ...

  5. vsftpd基于mysql的认证方式

    安装epel源: cd /etc/yum.repos.d wget http://mirrors.neusoft.edu.cn/epel/epel-release-latest-6.noarch.rp ...

  6. 20144303 《Java程序设计》第一周学习总结

    20144303 <Java程序设计>第一周学习总结 教材学习内容总结 下载.安装.调试了JDK. JavaSE是各语言个应用平台的基础,分为四个主要的部分:JVE,JRE,JDK,和ja ...

  7. 原生的 promise 的局限性

    本文来自:https://ekyu.moe/article/limits-of-native-promise-and-async-await/ 众所周知,Nodejs 已原生支持 Promise 和 ...

  8. [BZOJ1058]报表统计

    Description 小Q的妈妈是一个出纳,经常需要做一些统计报表的工作.今天是妈妈的生日,小Q希望可以帮妈妈分担一些工 作,作为她的生日礼物之一.经过仔细观察,小Q发现统计一张报表实际上是维护一个 ...

  9. 爬虫之Requests: 让 HTTP 服务人类

    Requests: 让 HTTP 服务人类 虽然Python的标准库中 urllib2 模块已经包含了平常我们使用的大多数功能,但是它的 API 使用起来让人感觉不太好,而 Requests 自称 “ ...

  10. AngularJS Injector和Service的工作机制

    要了解angularJS里的injector和Service是如何工作的,需要阅读/src/auto/injector.js.另外要结合/src/loader.js才能明白它的应用场景. auto/i ...