MySQL stored procedure to create an instance, two-cycle execution timing of the result set and

Creating a stored procedure using navicat

 

The BEGIN 
    #Routine body goes here Wallpaper ... 
            the DECLARE the startTime DATETIME default DATE_SUB (now (), interval The 60 MINUTE); # starting one hour before the current time 
            the DECLARE endTime DATETIME default the NOW (); # End current time 
    
            DECLARE start_num is int ;  dECLARE END_NUM int ;  # declaration completion flag  the dECLARE End_flag int the DEFAULT 0 ; # define the variable s cursor coordinate the dECLARE s int the DEFAULT 0 ; the dECLARE scollector_id int ; the dECLARE skwHd DOUBLE ; the dECLARE skVarHd DOUBLE ; the dECLARE scollect_time datetime ; the dECLARE ecollector_id int ; the dECLARE ekwHd DOUBLE ; DOUBLE ekVarHd DECLARE ; DECLARE ecollect_time datetime; #声明游标 start_curosr DECLARE start_curosr CURSOR FOR select t.id as collector_id,t1.EPS1+t1.EPS2 as kwhd,t1.EQS1+t1.EQS2 as kVarhd,t1.collect_time from tb_collector_info t LEFT JOIN tb_electric_power_info t1 ON t.id = t1.collector_id where t.id not in (SELECT collector_id FROM tb_kwh_info WHERE start_time = startTime and end_time = endTime) AND ABS(TIMESTAMPDIFF(MINUTE,t1.collect_time,startTime)) <= 4 order by collector_id; #声明游标 end_curosr DECLARE end_curosr CURSOR FOR select t.id as collector_id,t1.EPS1+t1.EPS2 as kwhd,t1.EQS1+t1.EQS2 as kVarhd,t1.collect_time from tb_collector_info t LEFT JOIN tb_electric_power_info t1 ON t.id = t1.collector_id where t.id not in (SELECT collector_id FROM tb_kwh_info WHERE start_time = startTime and end_time = endTime) AND ABS(TIMESTAMPDIFF(MINUTE,t1.collect_time,endTime)) <= 4 order by collector_id; #设置终止标志 DECLARE CONTINUE HANDLER FOR NOT FOUND SET end_flag=1; #将结果集大小放入start_num select count(*) into start_num from(select t.id as collector_id,t1.EPS1+t1.EPS2 as kwhd,t1.EQS1+t1.EQS2 as kVarhd,t1.collect_time from tb_collector_info t LEFT JOIN tb_electric_power_info t1 ON t.id = t1.collector_id where t.id not in (SELECT collector_id FROM tb_kwh_info WHERE start_time = startTime and end_time = endTime) AND ABS(TIMESTAMPDIFF(MINUTE,t1.collect_time,startTime)) <= 4 order by collector_id) ast2; # Add to the result set size end_num select count (*) into end_num from (select t.id as collector_id, t1.EPS1 + t1.EPS2 as kwhd, t1.EQS1 + t1.EQS2 as kVarhd, t1.collect_time from the JOIN tb_electric_power_info the LEFT T T1 tb_collector_info the ON t.id = t1.collector_id WHERE t.id Not in (the WHERE tb_kwh_info the SELECT collector_id the FROM and the startTime = START_TIME END_TIME = endTime) the aND the ABS (TIMESTAMPDIFF (MINUTE, t1.collect_time, endTime)) <= Order by collector_id. 4) aS T2; # operation if not empty when the two start_num is> 0 and END_NUM> 0 THEN # cursor is opened the oPEN start_curosr; - a first cursor loop out_loop: the FETCH start_curosr the INTO lOOP scollector_id, skwHd , skVarHd, scollect_time; =. 1 End_flag the IF THEN the LEAVE out_loop; the END the IF ; the OPEN end_curosr; - second cursor loop inner_loop:LOOP FETCH end_curosr INTO ecollector_id,ekwHd,ekVarHd,ecollect_time; IF end_flag = 1 THEN LEAVE inner_loop; END IF; IF scollector_id=ecollector_id then INSERT INTO tb_kwh_info(collector_id,start_time,end_time,kWh,kVarh,type) VALUES(scollector_id,scollect_time,ecollect_time,ekwHd-skwHd,ekVarHd-skVarHd,1); END IF; END LOOP inner_loop; CLOSE end_curosr; -- 注意这里,停止循环标志 SET end_flag=0; END LOOP out_loop; CLOSE start_curosr; end if; END

 

 

 

 Create an event to call a stored procedure

 

 

 

 

 

 Open regular tasks

 

Guess you like

Origin www.cnblogs.com/tyroxyz/p/12530224.html