BEGIN
DECLARE usrid int;
DECLARE product_id VARCHAR(10000);
declare avgPrice FLOAT;
DECLARE days int;
DECLARE start_date datetime;
declare j int DEFAULT 0;
DECLARE i int DEFAULT 0;
DECLARE sum1 int DEFAULT 0;
declare test CURSOR for
SELECT a.csid,a.product_id,FORMAT(( a.order_amount/DATEDIFF(b.end_date, b.start_date)), 2 ) avgprice,
DATEDIFF(b.end_date, b.start_date) Days,b.start_date
FROM pay_trade_order a LEFT JOIN pay_vip_record b ON a.csid = b.csid
WHERE a.csid =(select id from um_consumer where phone_no="18800000000") AND a.`status` = "1";
SELECT count(*) into sum1 FROM pay_trade_order WHERE csid =(select id from um_consumer where phone_no="18800000000") AND status= "1";
open test;
REPEAT
fetch test into usrid,product_id,avgPrice,days,start_date;
REPEAT
INSERT into tmp_table values (usrid,product_id,avgPrice,date_add(start_date, interval i day));
set i=i+1;
until i>Days end REPEAT;
set i=0;
set j=j+1;
until j>=sum1 end REPEAT;
close test;
END
有bug当sql未查询出数据,执行时会报错,未异常处理