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.

1
  • You can do it using a combination of PREPARE and CONCAT(). But it's the MySQL equivalent of eval() so no, I wouldn't call it good practice Commented Dec 12, 2013 at 14:29

1 Answer 1

0
DECLARE @Name varchar(max)
SELECT @Name =`table_name` FROM `data_tables` WHERE `table_id`="1"
SELECT * FROM `@Name`
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.