有点晚了,但刚刚解决了同样的问题,这是我想出的存储过程,以防它帮助别人:
CREATE DEFINER=`whoever`@`wherever` PROCEDURE `copy_schema`(
IN in_oldDB varchar(256),
IN in_newDB varchar(256),
IN in_copyData int(1)
)
BEGIN
DECLARE v_finished INTEGER DEFAULT 0;
DECLARE v_tname varchar(100) DEFAULT "";
DECLARE curTable CURSOR FOR
SELECT `table_name`
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = in_oldDB
and TABLE_TYPE='BASE TABLE'
;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_finished = 1;
set @result=NULL;
# Create new schema if it doesn't exist
SET @sql = CONCAT('CREATE SCHEMA IF NOT EXISTS ',in_newDB,';');
PREPARE create_schema FROM @sql;
EXECUTE create_schema;
DEALLOCATE PREPARE create_schema;
# Loop over tables in old schema
OPEN curTable;
clone_tables: LOOP
# get next table name
FETCH curTable INTO v_tname;
# Quit if we're done
IF v_finished = 1 THEN LEAVE clone_tables; END IF;
# Clone the table
SET @sql = CONCAT("CREATE TABLE `", in_newDB, "`.`", v_tname, "` LIKE `", in_oldDB, "`.`", v_tname, "`;");
PREPARE clone_table FROM @sql;
EXECUTE clone_table;
DEALLOCATE PREPARE clone_table;
# Optionally copy data
#select v_tname; # This just gives some feedback in workbench for long-running copies
IF (in_copyData > 0) THEN
SET @sql = CONCAT("INSERT INTO `", in_newDB, "`.`", v_tname, "` SELECT * FROM `", in_oldDB, "`.`", v_tname, "`;");
PREPARE clone_data FROM @sql;
EXECUTE clone_data;
DEALLOCATE PREPARE clone_data;
END IF;
# Result message
SET @result = IFNULL(CONCAT(@result,',',v_tname),v_tname);
END LOOP clone_tables;
# Close cursor
CLOSE curTable;
# Print result message
SELECT CONCAT("Copied the following tables from ", in_oldDB, " to ", in_newDB, ": ", @result);
END