mysql double cursor cursor nested loop

BEGIN
    declare tmp bigint default 0;
    declare uploaddate,currenttime DATETIME;
    declare freetraffic,sales,trafficpc,trafficapp,activity,calculate varchar(32);
    declare recordid,calid,errorsInfo int(11);
    -- variables in the calculation process
    declare gmv,taxsales,priceindex,margin,guestsinglequantity,nontaxcost,Basket,goodsprice,priceadjustment double;
    declare ordernumber,totalsales int(11);
    declare goodscode varchar(10);
-- Define the first cursor to traverse the uncomputed records
    declare cur CURSOR FOR
SELECT
id,upload_date,jd_free_traffic,jd_sales,paid_traffic_pc,paid_traffic_app,promotion_activity,data_calculate
FROM
ec_upload_across
WHERE
data_calculate ='no'
AND
upload_date < NOW()
ORDER BY
upload_date;
-- Define the second cursor to traverse the item number
declare innrcur CURSOR FOR
SELECT
goods_code
FROM
ec_jd_sales
WHERE
date=uploaddate; -- define the cursor
declare continue handler for not found set tmp=1;
declare continue handler for sqlexception set errorsInfo =1;
    OPEN cur; -- open the cursor  
    FETCH NEXT from cur INTO recordid,uploaddate,freetraffic,sales,trafficpc,trafficapp,activity,calculate; -- the cursor goes down one step  
    WHILE(tmp=0)
    DO    
-- INSERT INTO ec_calculate_logs(start_time,record_id,deal_flag,status) values(NOW(),recordid,'N','N');-- 测试用
IF freetraffic ='yes' and freetraffic ='yes' and sales ='yes' and trafficpc ='yes' and trafficapp ='yes' and activity='yes'
THEN
START TRANSACTION;
set currenttime =NOW();
INSERT INTO ec_calculate_logs(start_time,record_id,deal_flag,status) values(currenttime,recordid,'N','N'); -- write a record in the ec_calculate_logs table
select cal_id INTO calid from ec_calculate_logs where record_id=recordid AND start_time = currenttime;  -- 生成的主键ID
-- start calculating
OPEN innrcur; -- open cursor
   FETCH NEXT from innrcur INTO goodscode; -- cursor one step down
WHILE(tmp=0)
DO    
-- Calculate GMV, Margin
select tax_sales into taxsales from ec_jd_sales where date = uploaddate and goods_code=goodscode;
if taxsales IS NOT NULL
THEN
select COUNT(*) into priceindex from ec_promotion_activity where date = uploaddate and goods_code=goodscode;
if priceindex=0
THEN
SET priceindex=1.2;
ELSE
select price_index into priceindex from ec_promotion_activity where date = uploaddate and goods_code=goodscode;
if priceindex IS NULL
THEN SET priceindex=1.2;
end IF;
end IF;
-- custom test exception
-- if goodscode = 1828002 THEN INSERT INTO ec_calculate_logs(start_time,record_id,deal_flag,status) values(currenttime,mx,'N','N'); end IF;
SET gmv=taxsales*priceindex;
update ec_jd_sales set GMV=gmv where date = uploaddate and goods_code=goodscode;
set margin=gmv-taxsales;
update ec_jd_sales set Margin=margin where date = uploaddate and goods_code=goodscode;
end IF;
-- Calculate customer orders
select COUNT(*) into ordernumber from ec_free_traffic where date = uploaddate and goods_code=goodscode;
select total_sales into totalsales from ec_jd_sales where date = uploaddate and goods_code=goodscode;
if ordernumber<>0
THEN
select order_number into ordernumber from ec_free_traffic where date = uploaddate and goods_code=goodscode;
if ordernumber IS NOT NULL OR ordernumber<>0
THEN
set guestsinglequantity=totalsales/ordernumber;
update ec_jd_sales set guest_single_quantity=guestsinglequantity where date = uploaddate and goods_code=goodscode;
-- Calculation of untaxed cost depends on customer order volume
select non_tax_cost into nontaxcost from ec_jd_sales where date = uploaddate and goods_code=goodscode;
set Basket=guestsinglequantity*nontaxcost;
update ec_jd_sales set basket=Basket where date = uploaddate and goods_code=goodscode;
end IF;
end IF;
-- Calculate the price adjustment of Jingdong (the price of the day)
select COUNT(*) into priceadjustment from ec_promotion_activity where price_adjustment IS NULL AND date = uploaddate and goods_code=goodscode;
IF priceadjustment=1
THEN
select goods_price into goodsprice from ec_jd_sales where date = uploaddate and goods_code=goodscode;
update ec_promotion_activity set price_adjustment=goodsprice where date = uploaddate and goods_code=goodscode;
end IF;
FETCH NEXT  from innrcur INTO goodscode;
END WHILE;
SET tmp=0;
   CLOSE innrcur; -- close the cursor
-- update the state of the related table
update ec_calculate_logs set end_time=NOW(),status='Y' where cal_id =calid;
UPDATE ec_upload_across SET data_calculate='yes' WHERE id=recordid;
update ec_calculate_logs set deal_flag='Y' where cal_id =calid;
-- Abnormal rollback occurs, submit normally
if errorsInfo=1
THEN
ROLLBACK;
INSERT INTO ec_calculate_logs(start_time,record_id,deal_flag,status) values(currenttime,recordid,'N','N');
SET tmp=1;
ELSE
commit;
end IF;
ELSE
SET tmp=1;
END IF;
       FETCH NEXT from cur INTO recordid,uploaddate,freetraffic,sales,trafficpc,trafficapp,activity,calculate;
    END WHILE;
    CLOSE cur; -- close the cursor  
END

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326486637&siteId=291194637
Recommended