MySQL dynamic table name within query

MySQL dynamic table name within query

am 16.05.2006 18:27:46 von Ryan Hubbard

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

Re: MySQL dynamic table name within query

am 16.05.2006 18:34:53 von Thomas Bartkus

wrote in message
news:1147796866.234772.142160@v46g2000cwv.googlegroups.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
>

Re: MySQL dynamic table name within query

am 16.05.2006 20:37:18 von Bill Karwin

ryanmhuc@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.