Reputation: 14751
I write this query and it has error what is problem?
prepare test from
'select * from ?';
what is problem?
what is it's correct form?
Upvotes: 2
Views: 230
Reputation: 115520
You can't just put a placeholder wherever you like. Parameter placeholders can appear only in those places inside the statement where you would normally expect an expression. In particular, you cannot use parameter placeholders to parameterize identifiers or entire statement structures. That's why your attempt fails.
Many more useful things regarding prepared statements and Dynamic SQL in MYSQL can be found in Roland Bouman's blog -> MySQL 5: Prepared statement syntax and Dynamic SQL.
If your intended use is something like:
prepare test
from
'select * from ?' ;
set @myt := 'myTable' ;
execute test
using @myt ;
it will simply not work. But you can bypass it with:
set @myt := 'myTable'
set @qtext := concat('select * from ',@myt) ;
prepare test
from @qtext ;
execute test ;
Upvotes: 4
Reputation: 42554
I've never tried having the table name as a variable. I'm not sure that's allowed. Try the following:
PREPARE test FROM "SELECT * FROM table_name WHERE column = ?";
This is kind of a stab in the dark, since you're not providing the error message.
Upvotes: 1