demo场景:
先根据钱包流水记录算出用户余额、然后重置用户余额
注意事项:
1、要游标赋值的变量名字不能喝游标select查询出来的字段名字一样,,否则无法赋值
2、如果使用repeat循环,在until isStop = 1 后面不要加;
#如果存在存储过程initBalance,则删除掉
drop procedure if exists initBalance;
#创建存储过程initBalance
create procedure initBalance()
begin
#声明变量、类型、默认值
declare user_id int;
declare user_balance bigint;
declare isStop int default 0;
#声明游标
declare totalData cursor for
select uid,sum(amount) as balance from yuenan.tb_wallet_log where wtype != 25 and uid in (53,523425) GROUP BY uid;
declare continue handler for not found set isStop = 1;
#打开游标
open totalData;
#赋值到变量中
fetch totalData into user_id,user_balance;
#循环游标
while isStop <> 1 do
#select user_id,user_balance;
update yuenan.tb_wallet set balance = user_balance where uid = user_id;
fetch totalData into user_id,user_balance;
end while;
#关闭游标
close totalData;
end