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()

posted @   DLLDLL  阅读(77)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示