Q:

rename table prepared statement

I'm attempting to write a stored procedure in MySQL that removes and renames tables based on a specified prefix (in this case 'migrate_'). The code should recursively rename all tables starting with 'migrate_' until none remain, however, I'm getting an error when trying to run it.
BEGIN

SELECT COUNT(*) INTO @num_migrate
FROM information_schema.tables a 
WHERE a.table_name LIKE 'migrate_%';

IF (@num_migrate > 0) THEN
    SELECT concat( 'USE `md_rails`; DROP TABLE IF EXISTS `', 
            Replace(tbls.table_name,'migrate_',''), '`; ', 
        'RENAME TABLE `', tbls.table_name, '` TO `', 
            Replace(tbls.table_name,'migrate_',''), '`;' ) INTO @b
    FROM information_schema.tables tbls
    WHERE tbls.table_name LIKE 'migrate_%' LIMIT 1;
    PREPARE stmt FROM @b;
    EXECUTE stmt;
    CALL handle_migrated_tables();
END IF;

END

The error I'm getting is:
Error Code: 1064. You have an error in your SQL syntax; check the manual
that corresponds to your MySQL server version for the right syntax to use 
near 'DROP TABLE IF EXISTS second_test; RENAME TABLE migrate_second_test TO 
second_tes' at line 1

I've tried running the code generated from the select statement on its own and it does what I need, but for some reason it doesn't like it in the prepared statement.
Any help would be greatly appreciated!

A:

Lashane had it right! In case anybody was wondering, here is the final (working) code:
CREATE PROCEDURE `handle_migrated_tables`()
    BEGIN
        SET max_sp_recursion_depth=50;

        SELECT COUNT(*) INTO @num_migrate
        FROM information_schema.tables a 
        WHERE a.table_name LIKE 'migrate_%';

        IF (@num_migrate > 0) THEN

            SELECT tbls.table_name INTO @tbl
            FROM information_schema.tables tbls
            WHERE tbls.table_name LIKE 'migrate_%' LIMIT 1;

            SET @a = concat('DROP TABLE IF EXISTS md_rails.', Replace(@tbl, 'migrate_', ''));
            PREPARE stmt FROM @a;
            EXECUTE stmt;
            DEALLOCATE PREPARE stmt;

            SET @b = concat('RENAME TABLE md_rails.', @tbl, ' TO ', Replace(@tbl, 'migrate_', ''));
            PREPARE stmt FROM @b;
            EXECUTE stmt;
            DEALLOCATE PREPARE stmt;        

            CALL handle_migrated_tables();
        END IF;

    END

