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;