关于mysql 存储过程内实现循环,在用repeat 时发现, 很容易因为done的值循环终止。为此研究了下 do while来实现 循环。
过程和repeat 大差不差,值得注意的是 多了一个循环次数,通过该变量去定义我们要循环的次数。
做了一个简易的demo,如下,有需要的可自行copy。
CREATE DEFINER=`root`@`%` PROCEDURE `overview_financial_data_yl`(dateTime varchar(100),regionId varchar(100))
BEGIN-- 定义变量 成本中心编码
DECLARE vcenter_code varchar(64);
DECLARE v_maxmonth varchar(64);DECLARE v_changeNum bigint(12);
DECLARE in_Num bigint(12);DECLARE dayInfo CURSOR FOR
select center_code
from big_data.sys_dept
where status=0 and del_flag=0 and dept_level='3' and dept_attr in('1','4')
and if(regionId=0,1=1,FIND_IN_SET(regionId,ancestors)); -- 循环次数
select count(1) into in_Num
from big_data.sys_dept
where status=0 and del_flag=0 and dept_level='3' and dept_attr in('1','4')
and if(regionId=0,1=1,FIND_IN_SET(regionId,ancestors));set zcws=0;
set zylrs=0;
set shzylr=0;set wbzylr=0;
set shcwsy=0;
set shcwsy=0;
set wbsycw=0;
set shcwzs=0;
set v_changeNum=0;OPEN dayInfo;SET @j=1;WHILE @j<=in_Num DOFETCH dayInfo INTO vcenter_code;-- 数据处理逻辑SET @j=@j+1;END WHILE;CLOSE dayInfo;select zcws;select zylrs;select shzylr;select shcwsy;select wbzylr;select wbsycw;select shcwzs;END
好了,到这里就结束了。