1

I am having trouble getting my prepare statement to run in MySQL 5.6.14; here is the block of code in question:

SET @backupDate = DATE(NOW());
SET @renameTable = CONCAT('RENAME TABLE activeDirectoryData TO actDirBackup-', @backupDate);
PREPARE goRenameTable FROM @renameTable;
EXECUTE goRenameTable;
DEALLOCATE PREPARE goRenameTable;

The script stops at the prepare statement, with the following error:

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 '-2013-11-06' at line 1

Any idea what is wrong here?

4

1 に答える 1

1

actDirBackup-値が由来する名前@backupDateは有効なテーブル名ではありません。次のようにエスケープする必要があります。

SET @renameTable = CONCAT('RENAME TABLE activeDirectoryData TO `actDirBackup-',
                          @backupDate, '`');
于 2013-11-06T13:54:56.517 に答える