Mysql之WHILE替代游标实现子母表三层遍历拷贝

背景:

在做测试用例管理的时候,需要做测试用例的拷贝,测试用例下挂载测试步骤子表,测试步骤下挂载测试数据子表,都是一对多的关系。涉及遍历母表根据母表每条记录对应的子表记录进行拷贝,一开始考虑用游标,但是之前做过erp知道游标的效率是比较低的,而且会独占表资源。所以想用while来做,google了一下相关资料,实现了代码,现将代码分享如下

 

 
 
  1. CREATE DEFINER=`root`@`%` PROCEDURE `copy_case`(param_caseid int(11))  
  2. BEGIN 
  3.      DECLARE maxCnt INT DEFAULT 0;    
  4.      DECLARE i INT DEFAULT 0;    
  5.      INSERT INTO tc_case(testcasename,description,caselevel,runner,groupid,classname,create_time)   
  6.             select concat(testcasename,'.01') testcasename,description,caselevel,runner,groupid,classname,now() create_time   
  7.             from tc_case where caseid =  param_caseid;  
  8.      set @id = LAST_INSERT_ID();  
  9.      /**插入测试步骤**/  
  10.      INSERT INTO tc_step(testcaseid,steptype,stepmethod,description,module,stepid,create_time,classname,oldstepid)   
  11.             select @id,steptype,stepmethod,description,module,stepid,now() create_time,classname,id   
  12.             from tc_step where testcaseid = param_caseid;  
  13.               
  14.     DROP TABLE IF EXISTS Gather_Data_Tmp;            
  15.     CREATE TEMPORARY TABLE Gather_Data_Tmp(    
  16.         `Tmp_Id` INT UNSIGNED NOT NULL AUTO_INCREMENT,    
  17.         `teststepid` int(11) NOT NULL,    
  18.         `oldstepid` int(11) NOT NULL,  
  19.         PRIMARY KEY (`Tmp_Id`)    
  20.     )ENGINE=MyISAM DEFAULT CHARSET=utf8;       
  21.                        
  22.                                                                
  23.     SET @tSql = concat('INSERT INTO Gather_Data_Tmp (`teststepid`,`oldstepid`)     
  24.                                             SELECT id, oldstepid     
  25.                                             FROM tc_step     
  26.                                             WHERE testcaseid =',@id);    
  27.     PREPARE gatherData FROM @tSql;    
  28.     EXECUTE gatherData;    
  29.     SELECT MIN(`Tmp_Id`) INTO i FROM Gather_Data_Tmp;    
  30.     SELECT MAX(`Tmp_Id`) INTO maxCnt FROM Gather_Data_Tmp;    
  31.     /**插入测试数据**/  
  32.     WHILE i <= maxCnt DO    
  33.         set @teststepid = (SELECT teststepid FROM Gather_Data_Tmp WHERE Tmp_Id = i);    
  34.         set @oldstepid = (SELECT oldstepid FROM Gather_Data_Tmp WHERE Tmp_Id = i);  
  35.           
  36.         INSERT INTO tc_step_data(value,k_key,create_time,type,description,stepid )   
  37.             select value,k_key,now() create_time,type,description,@teststepid   
  38.             from tc_step_data where stepid = @oldstepid;  
  39.         SET i = i + 1;    
  40.     END WHILE;  
 
 
  1. <span style="font-size: 18px; "></span><pre name="code" class="html">    COMMIT
 
 
  1. END;  

 


本文转自elbertchen 51CTO博客,原文链接:http://blog.51cto.com/linkyou/751993,如需转载请自行联系原作者

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值