mysql存储过程嵌套循环并分页处理数据
Posted HEWU
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql存储过程嵌套循环并分页处理数据相关的知识,希望对你有一定的参考价值。
业务背景:公司存证产品升级,随着数据量的增加,存证产品线按业务分表,导致以往的存证关联数据需要做数据同步更新。版本发布前,通过当前存储过程解决数据升级问题。
##创建存证文档关联情况下更新所用存储过程 CREATE PROCEDURE evi_doc_refs_eid_deal() begin declare pageNum int default 0; declare totalSize int default 0; declare pageSize int default 5; declare totalPage int DEFAULT 0; declare startIndex int default 0; select count(*) into totalSize from osv_service_evidence where type != 11 and type !=12 and type !=50 ; set totalPage = totalSize/pageSize; while ( pageNum<totalPage-1) do #对分页的起始下标计算,以便分页查询时使用 set startIndex = pageNum *pageSize; #开启事务 START TRANSACTION; #嵌套内部分页查询,通过游标处理分页查询结果记录 begin #声明变量 declare eviId varchar(50); declare evidenceEid varchar(50); declare done int default 0; declare mark varchar(2); declare eid_value varchar(50); declare type_value int default 0; declare eid_mark varchar(50); declare num int DEFAULT 0; #定义分页查询结果的游标 declare i_cur cursor for select id from osv_service_evidence where type !=11 and type !=12 and type !=50 limit startIndex,pageSize; declare continue handler for sqlstate ‘09000‘ set done = 1; open i_cur; fetch next from i_cur into eviId; WHILE (done = 0 ) do #判断每页中处理数据的下标index set num = num+1; select type into type_value from osv_service_evidence where id = eviId; select e.eid into eid_value from osv_service_evidence e where id = eviId; if(0 =type_value|null = eid_value) then fetch next from i_cur into eviId; end if; #数据处理 if type_value =40 then set mark = ‘O‘; set eid_mark=concat(mark,eid_value);update evi_doc_refs set docEid = eid_mark where docEviId = eviId; elseif type_value =11 then set mark = ‘S‘; set eid_mark=concat(mark,eid_value);update evi_doc_refs set docEid = eid_mark where docEviId = eviId;set done = 0; end if; #如果当前页处理的数据已达页记录数值,通过done退出内部循环,进入外部循环 IF num = pageSize THEN set done = 1; #否则继续当前循环 ELSEIF num !=pageSize THEN set done =0; fetch next from i_cur into eviId; END IF; END WHILE; #关闭游标 close i_cur; end ; COMMIT; #更新下一次处理的页码,+1 set pageNum = pageNum+1; end while; END;
call evi_doc_refs_eid_deal();
##创建出证订单表中存证编号为null的情况下更新所用存储过程
create procedure evi_order_refs_eid_deal()
begin
declare original_eviId varchar(50);
declare evidenceEid varchar(50);
declare done int default 0;
declare mark varchar(2);
declare eid_value varchar(50);
declare type_value int default 0;
declare eid_mark varchar(50);
# declare existence boolean ;
##
declare cur cursor for select eviId from evi_order_refs where evidenceEid is null order by createTime desc;
##异常处理(触发sql语句失败,执行结束)
declare continue handler for sqlstate ‘09000‘ set done = 1;
open cur;
##取出游标值至变量中
fetch next from cur into original_eviId;
repeat
if not done then
select type into type_value from osv_service_evidence where id = original_eviId;
#逻辑判断,如果eviId确实在存证表中无记录,直接忽略该条出证记录
select e.eid into eid_value from osv_service_evidence e where id = original_eviId;
#逻辑判断,如果eviId确实在存证表中无记录,直接忽略该条出证记录
if(0 =type_value|null = eid_value) then fetch next from cur into original_eviId;
end if;
if type_value =40 then set mark = ‘O‘;set eid_mark=concat(mark,eid_value);update evi_order_refs set evidenceEid = eid_mark where eviId = original_eviId;
elseif type_value =11 then set mark = ‘S‘;set eid_mark=concat(mark,eid_value);update evi_order_refs set evidenceEid = eid_mark where eviId = original_eviId;else set done = 0;
end if;
end if;
#if(done = 1) then leave cur;
##重新抓取数据进入循环
fetch next from cur into original_eviId;
##结束循环
until done end repeat;
##关闭游标
close cur;
end ;
以上是关于mysql存储过程嵌套循环并分页处理数据的主要内容,如果未能解决你的问题,请参考以下文章