'How to put table name as variable in select query without using prepare statement in mysql?
My code is working with this prepare statement but I don't want to use prepare statement... Please give me another solution
set @sql1 = concat('select *from ', @table_names,'');
PREPARE stmt1 from @sql1;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
Thanks to all
Solution 1:[1]
Identifiers (e.g. table names) must be fixed in the query before the query is parsed.
So you cannot use as a table name:
- a variable
- a parameter
- a string expression
- result of a subquery
- etc.
You may only do what you're doing — write the new query as a string, and include the dynamic parts as a variable or an expression. Then you have a string, and you can prepare & execute that string. The parser has no way of knowing which parts of that string were originally a variable or an expression.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Bill Karwin |
