mysql使用存储过程,自动生成新的表单

时间:2021-04-08 05:04:34
use mydb;

delimiter //

CREATE procedure create_table(In imax int)
BEGIN
DECLARE `@i` int(11);
    
DECLARE `@sqlstr` varchar(2560);
    declare `@date` date;
    declare `@name` varchar(22);
    SET `@i`= 0;
WHILE `@i` < imax DO
set `@date` = DATE_ADD(now(), INTERVAL `@i` DAY);
        set `@name` = date_format(`@date`,'%Y%c%d');
SET @sqlstr = CONCAT(
"CREATE TABLE if not exists game",
DATE_FORMAT(`@date`,'%Y%m%d'),
"(
`player_id` int(11) NOT NULL AUTO_INCREMENT,
`account_id` int(11) NOT NULL COMMENT '用户ID',
`personaname` varchar(50) NOT NULL COMMENT '昵称',
PRIMARY KEY (`player_id`)
)"
);
prepare stmt from @sqlstr;
execute stmt;

SET `@i` = `@i` + 1;
END WHILE;
END;
call create_table(10);
drop procedure create_table;