469,623 Members | 1,929 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,623 developers. It's quick & easy.

MySQL dynamic table name within query

Is it possible to have a dynamic table name within a query or a table
name that is a variable? This does not work but gives an example:

SELECT * FROM concat('table', 'name')
- OR -

SET @table = 'a';
SELCT * FROM @table

Of course the query can be constructed dynamically but does not exactly
fit the needs. The situation is very complicated and would just be
confusing to discuss here so please do not offer suggestions on how to
construct the query programically as that will not work for this
situation.

Thanks

May 16 '06 #1
3 49506
<ry******@yahoo.com> wrote in message
news:11**********************@v46g2000cwv.googlegr oups.com...
Is it possible to have a dynamic table name within a query or a table
name that is a variable?
No!
This does not work but gives an example:

SELECT * FROM concat('table', 'name')
- OR -

SET @table = 'a';
SELCT * FROM @table

Of course the query can be constructed dynamically but does not exactly
fit the needs. The situation is very complicated and would just be
confusing to discuss here so please do not offer suggestions on how to
construct the query programically as that will not work for this
situation.

Thanks

May 16 '06 #2
ry******@yahoo.com wrote:
Of course the query can be constructed dynamically but does not exactly
fit the needs. The situation is very complicated and would just be
confusing to discuss here so please do not offer suggestions on how to
construct the query programically as that will not work for this
situation.


Table names, column names, etc. cannot be dynamic in the way you
describe. This is not permitted by the SQL language, for many reasons.

For instance, there would be no way for the query optimizer to decide
which index(es) to use, if it doesn't know at parse time which tables
and columns are being queried.

Find another way to solve your problem.

Regards,
Bill K.
May 16 '06 #3
Actually, it is possible. What you need to do is first create a variable that will contain the name of your table, then put that into the sql statement, except don't put the variable within single quotes:

$dynamic_table_name = "table"; (of course, this is where you would make it dynamic)

then, unlike the usual method of putting varibles in mysql statements - '$dynamic_table_name' - leave off the single quotes (') and just put
$dynamic_table_name, i.e. "select * from $dynamic_table_name......




Is it possible to have a dynamic table name within a query or a table
name that is a variable? This does not work but gives an example:

SELECT * FROM concat('table', 'name')
- OR -

SET @table = 'a';
SELCT * FROM @table

Of course the query can be constructed dynamically but does not exactly
fit the needs. The situation is very complicated and would just be
confusing to discuss here so please do not offer suggestions on how to
construct the query programically as that will not work for this
situation.

Thanks
Jun 6 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Gordon | last post: by
2 posts views Thread by Martin Feuersteiner | last post: by
2 posts views Thread by larry777 | last post: by
reply views Thread by devrayhaan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.