MySQL 表名用变量代替报1064错误.
CREATE DEFINER=`offsee`@`%` PROCEDURE `pro_terminal_report`
()
BEGIN
declare _version int ;
declare _currDate date ;
declare _tableName varchar(25);
declare _sql varchar(1000);
declare cur_version cursor for select id from soft_version where calc_version = 1 order by ver asc;
open cur_version;
cur_version:loop
fetch cur_version into _version ;
set _sql = concat('select * from terminal_log_v',5);
prepare _stmt from _sql;
execute _stmt;
deallocate prepare _stmt;
end loop;
set _currDate = date_format(now(),'%Y%m');
insert into terminal_report(province , terminal_count , fee, terminalPayCount , arup , mnc , otime)
select province , count(1) as terminalCount , sum(fee)/100 as fee, count(distinct mobile) as terminalPayCount ,(sum(fee)/100)/count(distinct mobile) arup , mnc ,otime from
(
select province, id, fee , mobile , if(carrier=1,1,0) as mnc , date_format(order_time,'%Y%m') as otime from sync_order as so where date_format(order_time,'%Y%m') = _currDate union
select province, id, fee , mobile , if(carrier=1,1,0) as mnc , date_format(order_time,'%Y%m') as otime from sync_order_month as sm where date_format(order_time,'%Y%m') = _currDate
) t
where otime = _currDate
group by province order by fee desc;
/****提交事务****/
END$$