主要思路是先创建一个存储过程,然后创建event每天定时执行存储过程。
一、创建日期表存储过程
DELIMITER //
CREATE PROCEDURE proc_create_table_daily(table_name varchar(100))
begin
declare str_date char(8);
declare create_sql varchar(100);
declare rename_sql varchar(100);
set str_date=date_format(now(),"%Y%m%d");
select str_date;
set create_sql=concat('create table ',table_name,'_tmp like ',table_name,";");
select create_sql;
set rename_sql=concat('rename table ',table_name,' to ',table_name,'_',str_date,',',table_name,'_tmp to ',table_name,";");
select rename_sql;
set @create_sql=create_sql;
prepare p1 from @create_sql;
execute p1;
deallocate prepare p1;
set @rename_sql=rename_sql;
prepare p2 from @rename_sql;
execute p2;
deallocate prepare p2;
end//
二、创建event定时创建表
DELIMITER //
DROP EVENT IF EXISTS event_create_table_daily//
CREATE EVENT event_create_table_daily
ON SCHEDULE EVERY 1 day
STARTS '2011-03-12 01:00:01'
ENABLE
DO
BEGIN
call proc_create_table_daily('event_test');
END//
DELIMITER ;
在写event过程中遇到的一些问题: