mysql 存储过程do while

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 DO
						 
							FETCH 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

你可能感兴趣的:(mysql,sql,数据库)