mysq 存储过程 插入测试数据

本文介绍了一个用于批量插入测试数据的存储过程。该过程允许用户指定起始患者ID、患者总数、开始日期、结束日期及每天记录数等参数,以便自动生成符合要求的数据。通过这个过程,可以高效地为数据库填充测试数据。
CREATE DEFINER = 'root'@'%' PROCEDURE `proc_InsertTestData`(
        
IN iPatientIdBegin INTEGER(11),
        
IN iPersonsCount INTEGER(11),
        
IN dStartDate DATE,
        
IN dEndDate DATE,
        
IN iRecordCountPerDay INTEGER(11)
    )
    
NOT DETERMINISTIC
    
CONTAINS SQL
    SQL SECURITY DEFINER
    COMMENT 
''
BEGIN
declare tempPatientId int;
declare iPatientIndex int;
declare tempDateTimeDesc varchar(100);

declare tempDate date;
declare tempIndex int;

declare tempFiledATime DateTime;
declare tempSymptoms varchar(200);
declare tempIncentives varchar(200);
declare tempDrugs varchar(200);

declare tempGatherTime DateTime;
declare tempFiledB Int;
declare tempFiledC float(9,2);
declare tempFTimeFlag varchar(5);
declare tempFAVailFlag tinyint;

declare tempTime1 varchar(6);
declare tempTime2 varchar(200);

set tempDateTimeDesc = '03:00|07:00|13:00|17:00|19:00';
set tempTime2 = concat(tempDateTimeDesc , '|');
set tempSymptoms = '1|2';
set tempIncentives = '1|2';
set tempDrugs = '4|3|Ƭ;5|5|ml';

/* 0 from first to last patientId */
set iPatientIndex = 1;
while iPatientIndex <= iPersonsCount do
    
set tempPatientId = iPatientIdBegin + iPatientIndex;
    
set iPatientIndex = iPatientIndex + 1;
    
insert into gathertimesFiledB (PatientID,DateTimeDesc , RecordCreateTime)
    
values(tempPatientId , tempDateTimeDesc ,dStartDate);
end while/* 0 from first to last patientId */

/* 1 from first to last date */
set tempDate = dStartDate;
while tempDate <= dEndDate do
    
    
/* 1.1 from first to last patientId */
    
set iPatientIndex = 1;
    
while iPatientIndex <= iPersonsCount do
        
set tempPatientId = iPatientIdBegin + iPatientIndex;
        
set iPatientIndex = iPatientIndex + 1;
        
        
/* 1.2 record a day */
        
set tempIndex = 1;
        
while tempIndex <= iRecordCountPerDay do
            
            
/* Filed */
            
set tempFiledATime = cast(concat(tempDate,' ',tempIndex,':05:00'as datetime);
            
insert into tableA (PatientID , FiledATime ,Symptoms
                   ,SymptomsOther , Incentives , IncentivesOther , Drugs)
             
values(tempPatientId ,tempFiledATime ,tempSymptoms
                  ,
'',tempIncentives, '' ,tempDrugs);
                
            
/* FiledB unvalid */
            
set tempFTimeFlag = concat(lpad(tempIndex,2,'0'),':00');
            
            
if FIND_IN_SET(tempFTimeFlag,tempDateTimeDesc) <= 0 then             
                
set tempGatherTime = cast(concat(tempDate,' ',lpad(tempIndex,2,'0'),':10:00'as datetime);
                
set tempFiledB = FLOOR(250 + ( RAND() * 51 ) );
                   
set tempFiledC = Round(1 + (RAND() * 2) ,1);            
                
set tempFAVailFlag = "0";
                
insert into tableB(PatientID , GatherTime , FiledB , FiledC , FTimeFlag , FAVailFlag)
                
VALUES(tempPatientId, tempGatherTime , tempFiledB , tempFiledC , tempFTimeFlag , tempFAVailFlag);
            
end if;
            
            
set tempIndex = tempIndex + 1;           
            
        
end while/* 1.2 record a day */
        
        
/* 1.3 FiledB valid */
        
while LENGTH(tempTime2) > 0 do            
            
set tempTime1 = substring(tempTime2,1,5);
            
set tempTime2 = substring(tempTime2,7);  
            
set tempFiledB = FLOOR(250 + ( RAND() * 51 ) );
            
set tempFiledC = Round(1 + (RAND() * 2) ,1);  
            
set tempGatherTime = cast(concat(tempDate,' ',tempTime1,':00'as datetime);
            
set tempFTimeFlag = tempTime1;
            
set tempFAVailFlag = "1";
            
insert into tableB(PatientID , GatherTime , FiledB , FiledC , FTimeFlag , FAVailFlag)
            
VALUES(tempPatientId, tempGatherTime , tempFiledB , tempFiledC , tempFTimeFlag , tempFAVailFlag);
        
end while/* 1.3 FiledB valid */
        
    
end while/* 1.1 from first to last patientId */

    
set tempDate = date_add(tempDate, interval 1 day);
end while/* 1 from first to last date */

END;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值