mysql存储过程中游标遍历的方法:
create definer=`root`@`%` procedure `updstatus`()begindeclare 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 thenleave read_loop;end if;select exam_status into estatus from t_exam where exam_id = id ;if estatus =1 thenselect 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 thenupdate t_exam set exam_status = 2 where exam_id = id;elseif curtime >= endtime thenupdate t_exam set exam_status = 3 where exam_id = id;end if;elseselect now() into curtime;select exam_end_time into endtime from t_exam where exam_id = id ;if curtime >= endtime thenupdate t_exam set exam_status = 3 where exam_id = id;end if;end if; end loop; close examids;end
更多相关免费学习推荐:mysql教程(视频)
以上就是mysql存储过程中游标如何遍历的详细内容。
