Bug #38527 user-defined variable with USE statement
Submitted: 2 Aug 2008 7:06 Modified: 7 Aug 2008 20:36
Reporter: Murat Tutumlu Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: Documentation Severity:S3 (Non-critical)
Version:5.0.51a, 4.1, 5.0, 5.1, 6.0 bzr OS:Any (Win XP)
Assigned to: Paul DuBois CPU Architecture:Any
Tags: Use, user-defined variable

[2 Aug 2008 7:06] Murat Tutumlu
Description:
When we use user-defined variable with USE statement, it returns SQL syntax error unlike SELECT Statement.

How to repeat:
SET @uservar = "mydbname";
USE @uservar;

returns error in SQL syntax

SELECT @uservar;
returns mydbname
[2 Aug 2008 11:23] Sveta Smirnova
Thank you for the report.

Verified as described:

mysql> set @var='test';
Query OK, 0 rows affected (0.37 sec)

mysql> use @var;
ERROR 1049 (42000): Unknown database '@var'

Probably this is documentation bug as we have at http://dev.mysql.com/doc/refman/5.0/en/user-variables.html "User variables may be used in contexts where expressions are allowed. This does not currently include contexts that explicitly require a literal value, such as in the LIMIT  clause of a SELECT statement, or the IGNORE N LINES  clause of a LOAD DATA statement." and no mention about such limitation for USE either at link provided or http://dev.mysql.com/doc/refman/5.0/en/use.html
[7 Aug 2008 20:36] Paul DuBois
Thank you for your bug report. This issue has been addressed in the documentation. The updated documentation will appear on our website shortly, and will be included in the next release of the relevant products.

Added to http://dev.mysql.com/doc/refman/5.0/en/user-variables.html:

User variables are intended to provide data values. They cannot be
used to supply an identifier, such as in a context where a table or
database name is expected.
[7 Aug 2008 20:47] Paul DuBois
Amended addition to manual:

User variables are intended to provide data values. They cannot be
used to supply identifiers, such as in contexts where a table or
database name is expected, or reserved words such as SELECT. An 
exception to this principle is that if you are constructing a string
for use as a prepared statement to be executed later, user variables
can be referenced to provide any part of the statement.