CREATE DEFINER=`mycommcrm`@`%` PROCEDURE `insert_source_flag`(IN `tableName` varchar(60))
BEGIN
#原始欠费数据 暂依据 客户唯一标记,起始账期,结束账期,账务月份区分
declarev_start_time datetime DEFAULT now();
declare startAccountDate varchar(64); -- 起始账期
declare endAccountDate varchar(64); -- 结束账期
declare accountMonth varchar(64); -- 账务月份
declare onlyFlag int;
declarea int DEFAULT 0;
-- 遍历数据结束标志
declare ifEnd int DEFAULT false;
-- 声明游标
DECLARE cti_dialout_temp_curs cursor for (
select start_account_date,end_account_date,account_month,only_flag from cti_dialout_temp
);
-- 将结束标志绑定到游标
declare CONTINUE HANDLER FOR not found SET ifEnd = true;
-- 打开游标
open cti_dialout_temp_curs;
-- 循环遍历
read_loop: LOOP
-- 提取变量值
fetch cti_dialout_temp_curs into startAccountDate,endAccountDate,accountMonth,onlyFlag;
-- 判断是否退出循环
IF ifEnd THEN
LEAVE read_loop;
END IF;
--- 凭借sql
# 如果该条数据不存在于上月初始欠费表,则加上初始欠费标记. 依据条件: 开始账期,结束账期,账务月份,唯一标记
set @exesql = CONCAT('select count(1) into @count from ',tableName ,' where start_account_date= ',startAccountDate);
set @exesql = CONCAT(@exesql,' AND end_account_date= ',endAccountDate,' AND account_month= ',accountMonth);
set @exesql = CONCAT(@exesql,' AND only_flag= ',onlyFlag);
prepare stmt from @exesql;
execute stmt;
DEALLOCATE prepare stmt;
if @count<1 THEN
update
cti_dialout_temp
set source_flag= 1
where start_account_date=startAccountDate
AND end_account_date=endAccountDate
AND account_month=accountMonth
AND only_flag=onlyFlag;
end if;
set @count = 0;
-- 关闭循环
end Loop;
-- 关闭游标
close cti_dialout_temp_curs;
#加入本月的初始欠费数据
set @exesql = CONCAT('INSERT INTO ',tableName ,' select * from cti_dialout_temp a where a.source_flag =1');
prepare stmt from @exesql;
execute stmt;
DEALLOCATE prepare stmt;
SELECT concat('耗时:', unix_timestamp(now()) - unix_timestamp(v_start_time), '秒!');
END