mysql存储过程,游标实例
CREATE DEFINER=`root`@`%` PROCEDURE `vir`.`task_payment_byonlinedown`()
begin
declare _mobile varchar(12) ;
declare _amount decimal(18,4) default 0;
declare _reduce_amount decimal(18,4) default 0;
declare _is_part varchar(12) ;
declare _excute_status varchar(12) ;
declare stop_flag int DEFAULT 0;
declare _app_id varchar(100);
declare _bill_status varchar(12);
declare _app_status varchar(12);
declare _REDUCE_AMOUNT_temp decimal(18,4) default 0;
declare cur1 cursor for select mobile,amount,reduce_amount,is_part,excute_status from task_payment_byonlinedown where excute_status=1;
declare CONTINUE HANDLER FOR SQLSTATE '02000' SET stop_flag=1;
open cur1;
fetch cur1 into _mobile,_amount,_reduce_amount,_is_part,_excute_status;#读取数据到游标
while stop_flag<>1 DO#若游标有下一条记录,循环
select apply_id,REDUCE_AMOUNT into _app_id,_REDUCE_AMOUNT_temp from vir.biz_bill where mobile=_mobile order by create_date desc limit 1;
#部分还款
if(_is_part='0') then
set _app_status =11 ;
set _bill_status=3;
end if;
#全部还款
if(_is_part='1') then
set _app_status =8;
set _bill_status=1;
end if;
#1 biz_apply
UPDATE biz_apply set APPLY_STATUS=_app_status WHERE id = _app_id;
#2 biz_bill
UPDATE biz_bill set
LATE_REPAYMENT_DATE=sysdate(),
LATE_REPQYMENT_AMOUNT=_amount,
REAL_TOTAL_AMOUNT=_amount,
BILL_STATUS=_bill_status,
SETTLE_DATE=sysdate(),
REDUCE_AMOUNT = _REDUCE_AMOUNT_temp+_reduce_amount,
DEMO2 = concat('appid','_',_app_id,'_对公还款_支付宝_还款金额_',_amount,'_减免金额_',_reduce_amount,'_还款时间_',sysdate())
WHERE APPLY_ID = _app_id;
#3 BIZ_REDUCE
insert into BIZ_REDUCE (id,APPLY_ID,REDUCE_TOTAL_AMOUNT,CREATE_DATE) values
( CONVERT(substring(CAST(rand() as char),3, length(CAST(rand() as char))) ,SIGNED),_app_id ,_amount ,sysdate());
update task_payment_byonlinedown set excute_status=0 where excute_status=1 and mobile=_mobile;
fetch cur1 into _mobile,_amount,_reduce_amount,_is_part,_excute_status;
end while;
close cur1;
END