mysql存储过程中游标如何遍历

2023年 4月 18日 59.4k 0

mysql存储过程中游标遍历的方法:首先取值,取多个字段;然后遍历数据结束标志,将结束标志绑定到游标,代码为【DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;】。 mysql存储过程中游标

mysql存储过程中游标遍历的方法:首先取值,取多个字段;然后遍历数据结束标志,将结束标志绑定到游标,代码为【DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;】。

mysql存储过程中游标遍历的方法:

CREATE DEFINER=`root`@`%` PROCEDURE `updStatus`()
BEGIN
DECLARE startTime DATETIME;
DECLARE endTime DATETIME;
DECLARE curTime DATETIME;
DECLARE id VARCHAR(36);
DECLARE estatus VARCHAR(4);
-- 遍历数据结束标志
DECLARE done INT DEFAULT FALSE;
-- 游标
DECLARE examIds CURSOR FOR SELECT EXAM_ID FROM t_exam WHERE EXAM_STATUS = 1 or EXAM_STATUS = 2;
-- 将结束标志绑定到游标
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN examIds;
-- 遍历
read_loop: LOOP
-- 取值 取多个字段
FETCH NEXT from examIds INTO id;
IF done THEN
LEAVE read_loop;
END IF;
SELECT EXAM_STATUS INTO estatus FROM t_exam WHERE EXAM_ID = id ;
IF estatus =1 THEN
SELECT NOW() INTO curTime;
SELECT EXAM_START_TIME INTO startTime from t_exam WHERE EXAM_ID = id ;
SELECT EXAM_END_TIME INTO endTime from t_exam WHERE EXAM_ID = id ;
IF curTime >= startTime AND endTime > curTime THEN
UPDATE t_exam SET EXAM_STATUS = 2 WHERE EXAM_ID = id;
ELSEIF curTime >= endTime THEN
UPDATE t_exam SET EXAM_STATUS = 3 WHERE EXAM_ID = id;
END IF;
ELSE
SELECT NOW() INTO curTime;
SELECT EXAM_END_TIME INTO endTime from t_exam WHERE EXAM_ID = id ;
IF curTime >= endTime THEN
UPDATE t_exam SET EXAM_STATUS = 3 WHERE EXAM_ID = id;
END IF;
END IF;
END LOOP;

CLOSE examIds;
END

更多推荐:mysql教程(视频)

以上就是mysql存储过程中游标如何遍历的详细内容,更多请关注每日运维其它相关文章!

相关文章

Oracle如何使用授予和撤销权限的语法和示例
Awesome Project: 探索 MatrixOrigin 云原生分布式数据库
下载丨66页PDF,云和恩墨技术通讯(2024年7月刊)
社区版oceanbase安装
Oracle 导出CSV工具-sqluldr2
ETL数据集成丨快速将MySQL数据迁移至Doris数据库

发布评论