mysql存储过程中游标如何遍历-kb88凯时官网登录

时间:2020-10-28
阅读:
免费资源网 - https://freexyz.cn/

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
免费资源网 - https://freexyz.cn/
返回顶部
顶部
网站地图