码迷,mamicode.com
首页 > 数据库 > 详细

mysql存储过程嵌套循环并分页处理数据

时间:2017-09-15 02:10:21      阅读:263      评论:0      收藏:0      [点我收藏+]

标签:抓取   efault   sqlstate   取数   升级   分页查询   公司   font   end   

业务背景:公司存证产品升级,随着数据量的增加,存证产品线按业务分表,导致以往的存证关联数据需要做数据同步更新。版本发布前,通过当前存储过程解决数据升级问题。

##创建存证文档关联情况下更新所用存储过程
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存储过程嵌套循环并分页处理数据

标签:抓取   efault   sqlstate   取数   升级   分页查询   公司   font   end   

原文地址:http://www.cnblogs.com/HEWU10/p/7524044.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!