Michael Sheaver
Michael Sheaver

Reputation: 2119

MySQL Prepare statement syntax

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?

Upvotes: 1

Views: 229

Answers (1)

Mahmoud Gamal
Mahmoud Gamal

Reputation: 79929

The name actDirBackup- with the value coming from @backupDate isn't a valid table name, you have to escape it, something like this:

SET @renameTable = CONCAT('RENAME TABLE activeDirectoryData TO `actDirBackup-',
                          @backupDate, '`');

Upvotes: 1

Related Questions