今天在做分表时候,遇到需要创建100张表的情景,手动创建太麻烦,使用存储过程实现了,记录下。
- 创建存储过程函数:
DELIMITER $$
CREATE PROCEDURE `createRecordTables`()
begin
declare i int;
set i=1;
while i<100 do
IF i<10 THEN
set @tb = CONCAT('create table table0',i,' like crm_customer_record00');
ELSE
set @tb = CONCAT('create table table',i,' like crm_customer_record00');
END IF;
PREPARE create_stmt FROM @tb;
EXECUTE create_stmt;
set i=i+1;
end while;
end $$;
DELIMITER ;
- 执行函数创建表:
CALL createRecordTables();