备份存储过程仅复制一个表

时间:2014-07-14 14:10:35

标签: mysql stored-procedures

我一直在尝试创建一个将我的数据库复制到另一个数据库的存储过程。问题是只有第一个表正在复制。你们看到代码中有任何错误吗?

     DELIMITER $$

     DROP PROCEDURE IF EXISTS fes_backup  $$

     CREATE PROCEDURE  do_backup (IN name_bbdd VARCHAR(20), IN new_name_bbdd VARCHAR(20))
     BEGIN

        DECLARE done INT DEFAULT 0;
        DECLARE name_table VARCHAR(20);

        DECLARE cur1 CURSOR FOR SELECT table_name FROM information_schema.tables
        WHERE table_schema=name_bbdd AND table_type="base table";

        DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;

        OPEN cur1;

        bucle: LOOP

           FETCH cur1 INTO name_table;
           IF done=1 THEN
              LEAVE bucle;
           END IF;

           SELECT CONCAT("Copio ",name_table," a nova base de dades ",new_name_bbdd);

           SET @aux = CONCAT("CREATE TABLE ",name_table," LIKE ", name_bbdd, ".", name_table);
           PREPARE stmt1 FROM @aux;
           EXECUTE stmt1;
           DEALLOCATE PREPARE stmt1;

           SET @aux = CONCAT("INSERT INTO ",name_table," SELECT * FROM ", name_bbdd, ".", name_table);
           PREPARE stmt1 FROM @aux;
           EXECUTE stmt1;
           DEALLOCATE PREPARE stmt1;

        END LOOP bucle;

        CLOSE cur1;

     END$$

     DELIMITER ;

非常感谢!

0 个答案:

没有答案