By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,269 Members | 1,332 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,269 IT Pros & Developers. It's quick & easy.

Create database using variable name

P: n/a
Hello,

I am using an SQL script to create a temporary database backup. While
there may be "other" or "better" ways to do this - I am really only
asking if the syntax (with tweaking) is possible - and if so what it
is.

Here is a very stripped down version of what I'm trying to attempt:

set @backUpName = 'myBackup';
create database @backUpName;
This throws a syntax error, but I don't know why.

Thanks!
CF

Sep 13 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
de**@chronofish.com wrote:
: Hello,

: I am using an SQL script to create a temporary database backup. While
: there may be "other" or "better" ways to do this - I am really only
: asking if the syntax (with tweaking) is possible - and if so what it
: is.

: Here is a very stripped down version of what I'm trying to attempt:

: set @backUpName = 'myBackup';
: create database @backUpName;

: This throws a syntax error, but I don't know why.

I thought I knew why, but wanted to confirm it. I googled the mysql SET
command, and it had a link to "user variables".
MySQL Reference Manual :: 9.3 User Variables

...
User variables may be used where expressions are allowed. This
does not currently include contexts that explicitly require a
literal value

In other words, things like
SELECT * FROM table WHERE col1 = @a_value;
will work but
SELECT * FROM @table_name;
will not work because the table name must be a literal value, not an
expression.

(caveat, mysql version not taken into account)
--

This programmer available for rent.
Sep 13 '05 #2

P: n/a
Malcolm Dew-Jones wrote:
User variables may be used where expressions are allowed. This
does not currently include contexts that explicitly require a
literal value

In other words, things like
SELECT * FROM table WHERE col1 = @a_value;
will work but
SELECT * FROM @table_name;
will not work because the table name must be a literal value, not an
expression.


Well, to be accurate, a table name is not a literal value. A table name
is a reference to some schema object. A literal value is something like
"abc" or 12.

I would guess (without having tried it ;-) that the comment about
literal values refers to contexts like the arguments of the LIMIT
clause, which take literal integer values, but not expressions or
parameters. That is, the following would not be permitted:
SELECT * FROM table LIMIT @a, @b;

Regards,
Bill K.
Sep 13 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.