MySQL:建立含游标的存储过程

#show procedure status where db='selfexam';  #查询某数据库里的存储过程


delimiter $$
DROP procedure 
IF EXISTS proc_update_examstate$$
CREATE procedure proc_update_examstate(in uid varchar(36))
BEGIN

DECLARE eid VARCHAR(36);
DECLARE btime datetime;
DECLARE etime datetime;
DECLARE estate int;
DECLARE stop int default 0;#结束标志
DECLARE cur_select_examstate CURSOR FOR 
select ExamId,ExamState,BeginTime,EndTime
from exam where CreatId = uid and ExamState in(0,1);
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET stop=1;

open cur_select_examstate;
fetch cur_select_examstate into eid,estate,btime,etime;
while stop <> 1 do 
     if (estate = 0 && now() >= btime) then 
         update exam set ExamState = 1 where ExamId = eid; 
     end if;
     if(estate = 1 && now() >= etime) then
         update exam set ExamState = 4 where ExamId = eid;   
     end if; 
     if (estate = 0 && now() >= etime) then 
        update exam set ExamState = 4 where ExamId = eid; 
     end if;
     fetch cur_select_examstate into eid,estate,btime,etime;
 end while;

close cur_select_examstate;

End$$
delimiter ;

#call proc_update_examstate('3307518b-1db9-4a9b-bb42-0dbb0e92afef');#调用存储过程

err[1265]:检查传入参数的类型是否正确
delimiter:默认情况下,delimiter是分号;。在命令行客户端中,如果有一行命令以分号结束,那么回车后,mysql将会执行该命令。但有时候,不希望MySQL这么做。可能输入较多的语句,且语句中包含有分号。delimiter $$,表示在出现$$的时候,表示语句执行。


转载于:https://my.oschina.net/laiwanshan/blog/178073

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值