0

In my application I want use dynamic table name.

In my application I have two queries.

1) SELECT `table_name` FROM `data_tables` WHERE `table_id`="1"

From this query I get a table name and I am saving it in a variable; for e.g: $tab

I have another query:

2) SELECT * FROM `'.$tab.'`;

I want to know is there anyway to club these queries? Is it a good practice?

My application is currently working fine, but I would like some insight.

homework
  • 4,987
  • 11
  • 40
  • 50
Nevin Thomas
  • 806
  • 4
  • 12
  • 24
  • You can do it using a combination of [`PREPARE`](http://dev.mysql.com/doc/refman/5.1/en/prepare.html) and [`CONCAT()`](http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_concat). But it's the MySQL equivalent of `eval()` so no, I wouldn't call it good practice – DaveRandom Dec 12 '13 at 14:29

1 Answers1

0
DECLARE @Name varchar(max)
SELECT @Name =`table_name` FROM `data_tables` WHERE `table_id`="1"
SELECT * FROM `@Name`
Andrii Horda
  • 170
  • 1
  • 16