存储过程

存储过程

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
1、IVR记录
2、技能组记录
3、其他(黑名单ID、区域路由ID、时间路由ID)',
				  `name` varchar(10) DEFAULT '',
				  `app` varchar(16) NOT NULL COMMENT '1、IVR(bustype =1)
ivr
addme
playcontinue

2、技能组(bustype =2)
group 
agent 
overtime
overqueue
notworktime
allnotlogin

3、其他(bustype =3)
time
area
blacklist
agent
extension
outline
playback
dtmf
hangup',
				  `apptype` tinyint(4) NOT NULL COMMENT '1、IVR
1 转入IVR
2 次数累加
3 播放继续

2、技能组
1 转接技能组
2 开始排队
3 准备呼叫坐席
4 呼叫坐席结果(成功/失败)
5 排队超时
6 排队溢出
7 非工作时间
8 所有坐席未登录

3、
1 时间路由
2 区域路由
3 黑名单
4 转呼坐席
5 转呼坐席结果
6 转呼分机
7 转呼分机结果
8 转呼外线
9 转呼外线结果
10 播放音乐
11 获取dtmf
12 挂机
',
				  `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

  

原文地址:https://www.cnblogs.com/lianzhilei/p/11384586.html