store procedure :
DELIMITER $$
CREATE DEFINER=`root`@`%` PROCEDURE `sp_u_nextmonth_table`(OUT p char(15))
BEGIN
Declare MM char(6);
SELECT DATE_FORMAT(DATE_ADD(now(),INTERVAL 1 MONTH),'%Y%m') INTO MM; /* search next month -- YYYYMM */
set p = concat('nexttable_',MM); /* 合併字串 */
END $$
DELIMITER ;
===================================================
DELIMITER $$
CREATE DEFINER=`root`@`%` PROCEDURE `sp_u_create_nexttable`()
BEGIN
/* call store procedure */
CALL sp_u_nextmonth_table(@ym);
SET @dyn_sql = concat('CREATE TABLE `testDB`.',@ym,'( `id` varchar(16) COLLATE utf8_unicode_ci NOT NULL DEFAULT '''',',
'`areaid` varchar(16) COLLATE utf8_unicode_ci NOT NULL DEFAULT '''',',
'KEY `idpk` (`id`)',
') ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci PACK_KEYS=1' );
/* select @dyn_sql; */
PREPARE s1 from @dyn_sql;
EXECUTE s1;
END $$
DELIMITER ;
crontab : (mysqlcrontab.sh)
/usr/local/bin/mysql -uroot -pxxxx -e "CALL TESTDB.sp_u_create_nexttable()"
沒有留言:
張貼留言