mysql database, using the variable name as the stored procedure table

    In the real business of them, there may be late to generate a table (products) backup data each month, this time we might have a table name generated by the table at the end of each month to back up data state, so think most well contains date information, such as products202003,

 create procedure proc_name()

 begin

    DECLARE nowtime char(10);

    DECLARE table_name char(30);
   SELECT DATE_FORMAT(NOW(),'%Y%m') into nowtime;
    select CONCAT('products',nowtime) into table_name;
    set @sqlStr=CONCAT('CREATE table if not exists ',table_name,' like products');
   PREPARE stmt from @sqlStr;
    EXECUTE stmt;

  set @insertSql=CONCAT('INSERT INTO ',table_name,' select * from products');
  PREPARE stmt2 from @insertSql;
  EXECUTE stmt2;

end

This procedure generates a table stored products named reconnection date data on the table, and then copy the data from the master table into the current data table

Guess you like

Origin www.cnblogs.com/zhengyixin/p/12502395.html