mysql定时创建月表

最后更新于:2022-04-01 21:48:16

MySQL定时创建表的SQL语句 -- 查看事件计划 SELECT @@event_scheduler -- 打开事件计划 www.2cto.com SET GLOBAL event_scheduler = 1 -- 存储过程,每月创建一张表 CREATE PROCEDURE create_table() BEGIN declare str_date varchar(16); SET str_date = date_format(now(),"%Y%m"); SET @sqlcmd1 = CONCAT('CREATE TABLE db_mail_message_',str_date,"(apptaskid varchar(100) NOT NULL,taskname varchar(200) NOT NULL,appsenduid varchar(200) NOT NULL,appreceiveuid varchar(100) NOT NULL,sendtime varchar(50) NOT NULL,handletime varchar(50) NOT NULL,url varchar(500) NOT NULL,remark varchar(500) NOT NULL,endtime varchar(50) NOT NULL,imapuid varchar(100) NOT NULL,addstatus int(1) NOT NULL DEFAULT '0',readstatus int(1) NOT NULL DEFAULT '0',deletestatus int(1) NOT NULL DEFAULT '0',completestatus int(1) NOT NULL DEFAULT '0',PRIMARY KEY (apptaskid),KEY db_imapuid_index (imapuid)) ENGINE=MyISAM DEFAULT CHARSET=utf8;"); PREPARE p1 FROM @sqlcmd1; EXECUTE p1; DEALLOCATE PREPARE p1; END -- 创建事件:每隔一秒自动调用存储过程 CREATE EVENT IF NOT EXISTS e_test ON SCHEDULE EVERY 1 SECOND ON COMPLETION PRESERVE DO CALL test(); www.2cto.com -- 创建事件:每月1号1点自动调用存储过程(未测试) CREATE EVENT if not exists e_test on schedule every 1 MONTH STARTS DATE_ADD(DATE_ADD(DATE_SUB(CURDATE(),INTERVAL DAY(CURDATE())-1 DAY), INTERVAL 1 MONTH),INTERVAL 1 HOUR) on completion preserve do call test(); -- 开启事件 alter event e_test ON COMPLETION PRESERVE ENABLE; -- 关闭事件 alter event e_test ON COMPLETION PRESERVE DISABLE;
';