MySQL預編譯,建立 下一個月按日期命名的表prepare
create DATABASE mydb2
use mydb2
create PROCEDURE proc_13_prepare()
begin
DECLARE next_year int;
DECLARE next_month int;
DECLARE next_month_day int;
DECLARE next_month_str varchar(2);
declare next_month_day_str varchar(2);
DECLARE table_name_str varchar(10);
declare t_index int DEFAULT 1;
set next_year=year(date_add(now(),INTERVAL 1 month));
set next_month=month(date_add(now(),INTERVAL 1 month));
set next_month_day=dayofmonth(LAST_DAY(DATE_ADD(now(),INTERVAL 1 month)));
if next_month<10
then set next_month_str=concat('0',next_month);
else
set next_month_str=concat('',next_month);
end if;
WHILE t_index <= next_month_day DO
if(t_index<10)
then set next_month_day_str=concat('0',t_index);
else
set next_month_day_str=concat('',t_index);
end if;
set table_name_str=concat(next_year,'_',next_month_str,'_',next_month_day_str);
set @create_table_sql=concat(
'create table user_',table_name_str,'(`uid` int,`ename` varchar(50),`information` varchar(50))
collate=\'utf8_general_ci\' ENGINE=InnoDB');
prepare create_table_stmt from @create_table_sql;
execute create_table_stmt;
DEALLOCATE prepare create_table_stmt;
set t_index=t_index+1;
END WHILE;
end
select dayofmonth(LAST_DAY(DATE_ADD(now(),INTERVAL 1 month)));
call proc_13_prepare()