mysql存储过程中游标怎样遍历
这篇文章主要介绍了MySQL存储过程中游标怎样遍历,具有一定借鉴价值,需要的朋友可以参考下。希望大家阅读完这篇文章后大有收获。下面让小编带着大家一起了解一下。
成都创新互联专注于企业成都营销网站建设、网站重做改版、石狮网站定制设计、自适应品牌网站建设、H5场景定制、成都做商城网站、集团公司官网建设、外贸网站制作、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为石狮等各大城市提供网站开发制作服务。
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存储过程中游标怎样遍历
网页链接:http://lswzjz.com/article/jpdgsc.html