Created
December 6, 2014 01:30
-
-
Save stanlemon/99374a439a616c765dcc to your computer and use it in GitHub Desktop.
Migrate tables from one database to another
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DROP PROCEDURE IF EXISTS migrate_db; | |
DELIMITER // | |
CREATE PROCEDURE migrate_db(IN dbnameFrom CHAR(255), IN dbnameTo CHAR(255)) | |
BEGIN | |
DECLARE done INT DEFAULT FALSE; | |
DECLARE tableName CHAR(255); | |
DECLARE tableCursor CURSOR FOR SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = dbnameFrom; | |
OPEN tableCursor; | |
tablesLoop: LOOP | |
IF done THEN | |
LEAVE tablesLoop; | |
END IF; | |
FETCH tableCursor INTO tableName; | |
SET @createTable = CONCAT('CREATE TABLE ', dbnameTo, '.', tableName, ' LIKE ', dbnameFrom, '.', tableName); | |
SET @insertTable = CONCAT('INSERT ', dbnameTo, '.', tableName, ' SELECT * FROM ', dbnameFrom, '.', tableName); | |
PREPARE createStmt FROM @createTable; | |
EXECUTE createStmt; | |
DEALLOCATE PREPARE createStmt; | |
PREPARE insertStmt FROM @insertTable; | |
EXECUTE insertStmt; | |
DEALLOCATE PREPARE insertStmt; | |
END LOOP; | |
CLOSE tableCursor; | |
END // |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment