Qgruber
Qgruber

Reputation: 145

How to use the name of the current database to delete it?

I try to assign "DATABASE()" which returns the name of the current database to a variable "@mydb" and to use it back to drop this database.

SET @mydb = DATABASE();
DROP DATABASE @mydb;

And i got this error ER_PARSE_ERROR near @mydb

Upvotes: 0

Views: 78

Answers (1)

nbk
nbk

Reputation: 49385

As P.Salmon mentioned you have to use Prepared Statements:

SET @mydb = DATABASE();
SET @sql = CONCAT('DROP DATABASE ', @mydb);
PREPARE stmt1 FROM  @sql;
EXECUTE stmt1;

Upvotes: 1

Related Questions