下面的存儲過程使用::
DELIMITER $$
CREATE
PROCEDURE `Rename_Tables`()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE tableName VARCHAR(50);
DECLARE newTableName VARCHAR(70);
DECLARE t_query VARCHAR(500);
DECLARE cur1 CURSOR FOR SELECT table_name FROM information_schema.tables WHERE table_name LIKE '%table_test' AND table_schema='test' ;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur1;
read_loop: LOOP
FETCH cur1 INTO tableName;
IF done THEN
LEAVE read_loop;
END IF;
SET newTableName = SUBSTRING(tableName, 1,LOCATE('_',tableName)-1);
SET t_query = CONCAT('RENAME TABLE ', tableName, ' TO ', newTableName);
SET @myQuery = t_query;
PREPARE stmt FROM @myQuery;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END LOOP;
END$$
DELIMITER;
用您的table_schema名稱替換table_schema。
可能超出了您的問題範圍,但您可以編寫一個腳本來轉儲表列表,在腳本中執行匹配,然後執行'RENAME'。 – 2012-07-25 03:01:18
假設我可以編寫腳本來獲取這些表名稱的列表,那麼我該怎麼做?你能解釋更多嗎?謝謝 – user1550338 2012-07-25 03:11:42