重命名 table 准备好的语句
rename table prepared statement
我正在尝试在 MySQL 中编写一个存储过程,它根据指定的前缀(在本例中为 'migrate_')删除和重命名表。代码应该递归地重命名所有以 'migrate_' 开头的表,直到 none 保留,但是,我在尝试 运行 时遇到错误。
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
我得到的错误是:
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
我已经尝试 运行 单独使用 select 语句生成的代码,它满足了我的需要,但出于某种原因,它不喜欢准备好的语句.
如有任何帮助,我们将不胜感激!
Lashane答对了!如果有人想知道,这里是最终(工作)代码:
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
我正在尝试在 MySQL 中编写一个存储过程,它根据指定的前缀(在本例中为 'migrate_')删除和重命名表。代码应该递归地重命名所有以 'migrate_' 开头的表,直到 none 保留,但是,我在尝试 运行 时遇到错误。
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
我得到的错误是:
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
我已经尝试 运行 单独使用 select 语句生成的代码,它满足了我的需要,但出于某种原因,它不喜欢准备好的语句.
如有任何帮助,我们将不胜感激!
Lashane答对了!如果有人想知道,这里是最终(工作)代码:
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