当前位置: 首页 > news >正文

深刻理解MySQL8游标处理中not found

深刻理解MySQL8游标处理中not found

最近使用MySQL的游标,在fetch循环过程中,程序总是提前退出 ,百思不得其解,经过测试,原来是对于游标处理中not found的定义理解有误,默认是视同Oracle的游标not found定义,结果思考测试了两天,终于走出了思维定式。

1. 问题描述

MySQL版本,8.0.16 。

存储过程如下:

CREATE DEFINER=`root`@`%` PROCEDURE `pro_test_nofound_cursor`()
begindeclare v_done int default 1 ;declare v_name varchar(10);declare v_date date;declare v_string text;declare v_for_nofound varchar(10);declare v_counter int default 0;declare cur_stud1 cursor for select t.name ,t.birthday from tb_student t where t.grade >= 70 and t.grade < 80 order by t.grade desc limit 3;declare continue handler for not found set v_done = 0;#使用游标前打开游标open cur_stud1 ;set v_string = '';cur_loop: loopfetch next from cur_stud1 into v_name ,v_date;set v_counter = v_counter + 1;if v_done = 0 then leave cur_loop;end if;-- 此查询无结果,是空。	select t.name  into v_for_nofound 	from tb_student t where t.grade >= 101  order by t.grade desc limit 1;set v_string = concat(v_string,' stud1:',v_name , ' :',v_date);end loop cur_loop;close cur_stud1 ;select v_string;select v_counter;end

游标记录是3条记录,但是查询结果,只反馈一条记录值。
游标理解应该循环3次!!!,但是只返回了一条记录。
为什么 ???

结果如下:

mysql> call pro_test_nofound_cursor();
+-------------------------------+
| v_string                      |
+-------------------------------+
|  stud1:CJXBCEXCOF :2023-09-18 |
+-------------------------------+
1 row in set (0.00 sec)Query OK, 0 rows affected (0.00 sec)mysql> call pro_test_nofound_cursor();
+-------------------------------+
| v_string                      |
+-------------------------------+
|  stud1:CJXBCEXCOF :2023-09-18 |
+-------------------------------+
1 row in set (0.00 sec)+-----------+
| v_counter |
+-----------+
|         2 |
+-----------+
1 row in set (0.00 sec)Query OK, 0 rows affected (0.00 sec)

结果说明:
记录返回:只有1条
计数器:是2

2. 问题分析

MySQL文档:
MySQL定义not found的说明

NOT FOUND: Shorthand for the class of SQLSTATE values that begin with ‘02’. This is relevant within the context of cursors and is used to control what happens when a cursor reaches the end of a data set. If no more rows are available, a No Data condition occurs with SQLSTATE value ‘02000’. To detect this condition, you can set up a handler for it or for a NOT FOUND condition.

DECLARE CONTINUE HANDLER FOR NOT FOUND   BEGIN-- body of handler   END; 

For another example, see Section 13.6.6, “Cursors”. The NOT FOUND condition also occurs for SELECT … INTO var_list statements that retrieve no rows.

说明:
SQLSTATE value ‘02000’ 和 NOT FOUND 是等价的,那么NOT FOUND 就不是cursor所专属的状态值。因此在循环中,如果出现了查询没有结果的情况,那么将直接 触发v_done = 0 ,并非cursor的fetch 触发的结果。

注意:与Oracle游标访问的notfound状态值是不同的,oracle是专用于cursor,而MySQL是notfound状态是所有SQL共用的!!!

惯性思维,困扰了两天。

declare continue handler for not found set v_done = 0;

3. 问题解决

在游标循环中最后增加一行,强制设置为1 ;

set v_done = 1;

程序只有在fetch的时候,产生的v_done状态,才能触发退出循环。
修改后的程序如下:

CREATE DEFINER=`root`@`%` PROCEDURE `pro_test_nofound_cursor`()
begindeclare v_done int default 1 ;declare v_name varchar(10);declare v_date date;declare v_string text;declare v_for_nofound varchar(10);declare v_counter int default 0;declare cur_stud1 cursor for select t.name ,t.birthday from tb_student t where t.grade >= 70 and t.grade < 80 order by t.grade desc limit 3;declare continue handler for not found set v_done = 0;#使用游标前打开游标open cur_stud1 ;set v_string = '';cur_loop: loopfetch next from cur_stud1 into v_name ,v_date;set v_counter = v_counter + 1;if v_done = 0 then leave cur_loop;end if;-- 此查询无结果,是空。	select t.name  into v_for_nofound 	from tb_student t where t.grade >= 101  order by t.grade desc limit 1;set v_string = concat(v_string,' stud1:',v_name , ' :',v_date);set v_done = 1;end loop cur_loop;close cur_stud1 ;select v_string;select v_counter;end

执行结果:

mysql> call pro_test_nofound_cursor();
+-----------------------------------------------------------------------------------------+
| v_string                                                                                |
+-----------------------------------------------------------------------------------------+
|  stud1:CJXBCEXCOF :2023-09-18 stud1:FIDLSJAYFS :2023-11-08 stud1:KEVQMOCIEW :2023-09-06 |
+-----------------------------------------------------------------------------------------+
1 row in set (0.01 sec)+-----------+
| v_counter |
+-----------+
|         4 |
+-----------+
1 row in set (0.01 sec)Query OK, 0 rows affected (0.01 sec)

执行结果正确,返回了3条记录,计数器值是4 。

http://www.lryc.cn/news/241272.html

相关文章:

  • 甄知燕千云与SAP、EBS、TC、NS等应用深度集成,智能提单一键畅通,效能一键提升
  • 第99步 深度学习图像目标检测:SSDlite建模
  • 用EasyAVFilter将网络文件或者本地文件推送RTMP出去的时候发现CPU占用好高,用的也是vcodec copy呀,什么原因?
  • Vatee万腾科技的独特力量:Vatee数字时代创新的新视野
  • 【JavaSE】基础笔记 - 异常(Exception)
  • QTableWidget——编辑单元格
  • 编译QT Mysql库并集成使用
  • 利用企业被执行人信息查询API保障商业交易安全
  • 【深度学习】P1 深度学习基础框架 - 张量 Tensor
  • vue2 识别页面参数中的html
  • matlab 一些画图法总结(持续更新)
  • MDK AC5和AC6是什么?在KEIL5中添加和选择ARMCC版本
  • 杰发科技AC7801——EEP内存分布情况
  • 【mybatis注解实现条件查询】
  • 【广州华锐互动】VR线上课件制作软件满足数字化教学需求
  • MySQL 中 DELETE 语句中可以使用别名么?
  • flutter创建不同样式的按钮,背景色,边框,圆角,圆形,大小都可以设置
  • 【C++】标准模板库STL作业(其二)
  • 基于SpringBoot+Redis实现点赞/排行榜功能,可同理实现收藏/关注功能,可拓展实现共同好友/共同关注/关注推送功能
  • AI“胡说八道”?怎么解?
  • [SIGGRAPH-23] 3D Gaussian Splatting for Real-Time Radiance Field Rendering
  • 大话设计模式C++实现
  • IT 领域中的主要自动化趋势
  • 使用Python解析CAN总线
  • DevExpress中文教程 - 如何在macOS和Linux (CTP)上创建、修改报表(下)
  • RAID的应用场景以及优缺点
  • java SpringCloud版本b2b2c鸿鹄云商平台全套解决方案 小程序商城免费搭建
  • [Linux] shell脚本的函数和数组
  • 万宾科技智能井盖的效果怎么样?
  • nvm切换版本之后npm用不了