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
##创建出证订单表中存证编号为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存储过程嵌套循环并分页处理数据)