Diane
Diane

Reputation: 119

mysql stored procedures and parameters

I have a stored procedure which selects from a certain table. This table gets passed to the procedure as a parameter. SELECT * FROM ParamName And all it tells me when I try to run it is 'Table ParamName doesn't exist. Any suggestions are greatly appreciated?

Upvotes: 0

Views: 127

Answers (1)

Marc B
Marc B

Reputation: 360862

ParamName by itself would appear to MySQL as a table/field name, which it's rightly complaining about. If Paramname is a parameter for the stored procedure, you'd have to use @ParamName to tell MySQL to look for a variable of that name.

However, you can't simply do

SELECT * FROM @ParamName

mysql doesn't allow table/fieldnames to be specified via variables. You'll have to prepare a statement first:

SET @sql=CONCAT('SELECT * FROM ', @ParamName);
PREPARE s1 FROM @sql;
EXECUTE s1;

Upvotes: 1

Related Questions