SET Variable statement
I'm trying to write a SQL script that creates a database and a few other things.
I'm having trouble with a user defined variable statement.
SET @dbname = CAST(trees AS VARCHAR(10)); comes directly from https://mariadb.com/kb/en/mariadb/user-defined-variables/
And I keep getting --> Error: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'VARCHAR(10))' at line 1
Running MariaDB 10.0.20 on SLES 12, I have no idea what the problem is ?
Answer Answered by Elena Stepanova in this comment.
You need prepared statements to pull it off. Something like
SET @dbname = 'name'; SET @sql = CONCAT('drop database if exists ', @dbname); PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
Content reproduced on this site is the property of its respective owners,
and this content is not reviewed in advance by MariaDB. The views, information and opinions
expressed by this content do not necessarily represent those of MariaDB or any other party.