Vimes
Vimes

Reputation: 11927

MySQL User-Defined Variable Values Can't be Used as Identifiers. How do I do this?

According to the manual:

User variables... cannot be used directly in an SQL statement as an identifier or as part of an identifier, such as in contexts where a table or database name is expected

Which explains why what I've been trying doesn't work:

set @databaseName := 'job_hunt_2';

drop database @databaseName;
create database @databaseName;
use @databaseName;

Is there a way to accomplish this, or is it simply impossible? Thanks!

Upvotes: 2

Views: 841

Answers (1)

heximal
heximal

Reputation: 10517

may be you should try following approach:

set @databaseName := 'job_hunt_2';
SET @s = CONCAT('drop database ', @databaseName); 
PREPARE stmt1 FROM @s; 
EXECUTE stmt1; 
DEALLOCATE PREPARE stmt1;

Upvotes: 3

Related Questions