原!删除表存储过程

-- 处理存储过程
drop procedure if exists proc_drop_2017msg_tables;              -- 存储过程名称
delimiter //
create procedure proc_drop_2017msg_tables()                             -- 存储过程名称
  begin

    set @tab_year=17;                           -- 开始年份
    set @tab_month=09;                          -- 开始月份
    set @tab_day=20;                                    -- 开始日期

    while @tab_year<=17 do                      -- 结束年份
      while @tab_month<=12 do           -- 结束月份
        if (@tab_month=1 or @tab_month=3 or @tab_month=5 or @tab_month=7 or @tab_month=8 or @tab_month=10 or @tab_month=12) then
          set @days=31;
        elseif @tab_month=2 then
          if (@tab_year%400=0 or (@tab_year%4=0 and @tab_year%100<>0)) then
            set @days=29;
          else
            set @days=28;
          end if;
        else
          set @days=30;
        end if;

        while @tab_day<=@days do
          if @tab_month<10 then
            if @tab_day<10 then
              set @table_name=CONCAT('open_messageinfo_20',@tab_year,'0',@tab_month,'0',@tab_day);      -- 表名
            else
              set @table_name=CONCAT('open_messageinfo_20',@tab_year,'0',@tab_month,@tab_day);          -- 表名
            end if;
          else
            if @tab_day<10 then
              set @table_name=CONCAT('open_messageinfo_20',@tab_year,@tab_month,'0',@tab_day);          -- 表名
            else
              set @table_name=CONCAT('open_messageinfo_20',@tab_year,@tab_month,@tab_day);                      -- 表名
            end if;
          end if;
          set @alter_table_sql=concat('drop table ',@table_name,' ;');
          prepare statement from @alter_table_sql;
          execute statement;
          set @tab_day=@tab_day+1;
        end while;
        set @tab_month=@tab_month+1;
        set @tab_day=1;
      end while;
      set @tab_year=@tab_year+1;
      set @tab_month=1;
    end while;
  end
//
delimiter ;
call proc_drop_2017msg_tables;                  -- 存储过程名称

  

原文地址:https://www.cnblogs.com/wuyun-blog/p/10640975.html