存储过程
CREATE DEFINER=`root`@`%` PROCEDURE `segment_tx_callin_track`()
BEGIN
DECLARE _exists FLOAT(14,6) DEFAULT 0;
set @mouth= DATE_FORMAT(DATE_SUB(NOW(),INTERVAL 1 MONTH),'%Y%m');
# 新表名
set @new_table_name = concat('tx_callin_track_', @mouth);
# 表是否存在
set @new_table_exists = concat("SELECT count(1) into @tnum FROM information_schema.TABLES WHERE table_name = '",@new_table_name,"'");
PREPARE mte from @new_table_exists;
EXECUTE mte;
set _exists = @tnum;
if _exists = 0 then
# 创建新表
CREATE TABLE `tx_callin_track_new` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`compid` char(6) NOT NULL,
`callid` char(32) NOT NULL,
`busid` char(10) DEFAULT '' COMMENT '技能组ID / IVR ID / 黑名单ID、区域路由ID、时间路由ID',
`bustype` tinyint(4) NOT NULL COMMENT '对应busid\n1、IVR记录\n2、技能组记录\n3、其他(黑名单ID、区域路由ID、时间路由ID)',
`name` varchar(10) DEFAULT '',
`app` varchar(16) NOT NULL COMMENT '1、IVR(bustype =1)\nivr\naddme\nplaycontinue\n\n2、技能组(bustype =2)\ngroup \nagent \novertime\noverqueue\nnotworktime\nallnotlogin\n\n3、其他(bustype =3)\ntime\narea\nblacklist\nagent\nextension\noutline\nplayback\ndtmf\nhangup',
`apptype` tinyint(4) NOT NULL COMMENT '1、IVR\n1 转入IVR\n2 次数累加\n3 播放继续\n\n2、技能组\n1 转接技能组\n2 开始排队\n3 准备呼叫坐席\n4 呼叫坐席结果(成功/失败)\n5 排队超时\n6 排队溢出\n7 非工作时间\n8 所有坐席未登录\n\n3、\n1 时间路由\n2 区域路由\n3 黑名单\n4 转呼坐席\n5 转呼坐席结果\n6 转呼分机\n7 转呼分机结果\n8 转呼外线\n9 转呼外线结果\n10 播放音乐\n11 获取dtmf\n12 挂机\n',
`c1` varchar(40) DEFAULT '',
`c2` varchar(10) DEFAULT '',
`date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `cdate` (`date`),
KEY `callid` (`callid`(6))
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
# 旧表重命名
set @table_rename = concat("ALTER TABLE `cdr`.`tx_callin_track` RENAME TO ", @new_table_name);
PREPARE stmt from @table_rename;
EXECUTE stmt ;
# 新表重命名
ALTER TABLE `cdr`.`tx_callin_track_new` RENAME TO `cdr`.`tx_callin_track` ;
end if;
END