-- --------------------------------------------------------------------------------
-- Clone a db
-- Note: comments before and after the routine body will not be stored by the server
-- --------------------------------------------------------------------------------
DELIMITER $$
CREATE DEFINER=`dbname`@`%` PROCEDURE `clone_db`(IN old_db VARCHAR(100), IN new_db VARCHAR(100))
BEGIN
DECLARE current_table VARCHAR(100);
DECLARE done INT DEFAULT 0;
DECLARE old_tables CURSOR FOR select table_name from information_schema.tables where table_schema = old_db and table_type='BASE TABLE';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN old_tables;
REPEAT
FETCH old_tables INTO current_table;
IF NOT done THEN
SET @output = CONCAT('create table ', new_db, '.', current_table, ' like ', old_db, '.', current_table, ';');
PREPARE stmt FROM @output;
EXECUTE stmt;
SET @output = CONCAT('insert into ', new_db, '.', current_table, ' select * from ', old_db, '.', current_table, ';');
PREPARE stmt FROM @output;
EXECUTE stmt;
END IF;
UNTIL done END REPEAT;
CLOSE old_tables;
END
-- rename the databases
-- Note: comments before and after the routine body will not be stored by the server
-- --------------------------------------------------------------------------------
DELIMITER $$
CREATE DEFINER=`dbname`@`%` PROCEDURE `rename_db`(IN old_db VARCHAR(100), IN new_db VARCHAR(100))
BEGIN
DECLARE current_table VARCHAR(100);
DECLARE done INT DEFAULT 0;
DECLARE old_tables CURSOR FOR select table_name from information_schema.tables where table_schema = old_db;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN old_tables;
REPEAT
FETCH old_tables INTO current_table;
IF NOT done THEN
SET @output = CONCAT('alter table ', old_db, '.', current_table, ' rename ', new_db, '.', current_table, ';');
PREPARE stmt FROM @output;
EXECUTE stmt;
END IF;
UNTIL done END REPEAT;
CLOSE old_tables;
END
Comments
Post a Comment