MySQL 存储过程 游标

存储过程
-- 本存储过程有特殊执行循环数量的要求,是对security_market_history表进行修正
-- 判断存储过程是否存在
drop PROCEDURE if exists proc_security_market_history_update;
CREATE PROCEDURE proc_security_market_history_update()
begin
DECLARE p_i int default 0; -- 设置初始参数
set @p_days = date_sub(curdate(),interval 0 day); -- 设置需要初始化的值

repeat -- 开启循环




创建存储过程
create procedure proc_finance_send_charging()
begin
  declare cur_time VARCHAR(36);
  declare cur_charging_id VARCHAR(10);
  declare done int default 0;
 
  /* 声明游标 */
  declare cur cursor for select id from gt_finance_charginginfo where pay_type = 2 and pay_status = 0 
          and (type = 2 or type = 3) and create_time <= cur_time and create_time >= 1510122630000;
  /* 异常处理 */  
  declare continue handler for sqlstate '02000' set done = 1;


  set cur_time = unix_timestamp(now())* 1000 - 1000*60*30; 
  open cur;
    fetch next from cur into cur_charging_id;
    repeat
IF NOT Done THEN
update gt_finance_charginginfo set pay_status = 3 , update_time = unix_timestamp(now())*1000 where id = cur_charging_id;
update gt_finance_sendtask set finance_type = 0 ,finance_status = 0, charging_id = null,receive_time = null,
update_time = unix_timestamp(now())*1000,finance_pay_status = if(finance_pay_status = 1,1,0) 
where charging_id = cur_charging_id;
END IF;
FETCH NEXT FROM cur INTO cur_charging_id;
    until done end repeat;
  close cur;
end


执行定时任务
call proc_finance_send_charging()


删除定时任务
drop procedure proc_finance_send_charging

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值