mysql存储过程实现创建表的同时查找数据并输入

DELIMITER $$
CREATE PROCEDURE `creatAndInsert`()
BEGIN
        DECLARE `@i` INT(11);     
        DECLARE `@j` INT(11);
        DECLARE `@createSql` VARCHAR(2560); 
SET `@i`=1;
				WHILE  `@i`< 12 DO 
				SET `@j`=`@i`+3;
SET @createSql=CONCAT('create table table_',`@i`,' SELECT*FROM t_admin WHERE id BETWEEN ',`@i`,' and ',`@j`);
PREPARE stmt FROM @createSql; 
                            EXECUTE stmt; 
SET `@i`= `@i`+4; 
            END WHILE;
     

END

在下面call creatAndInsert()即可

你可能感兴趣的:(数据库)