Pass table name to procedure including fields

5 pts.
Tags:
MySQL
MySQL Database
I want to do the following with a procedure: I have per user that has the same structure. The only difference is the table name. I need to be able to pass the table name to a procedure and retrieve data for that table. At the moment I have this: sp_getdata(id, givendate datetime,tablename varchar(200)) BEGIN SELECT column1, column2, column3 FROM 'tablename', table2 WHERE 'tablename'.id = id AND 'tablename'.date = givendate; AND 'tablename'.uniqueid=table2.uniqueid END; table2 never change but tablename yes. Any idea how can I do this? thanks
ASKED: May 14, 2012  5:55 AM
UPDATED: May 16, 2012  9:18 AM

Answer Wiki

Thanks. We'll let you know when a new response is added.

Use a varible to create your statement.
Pass tablename as @tablename

declare @sql as varchar(1000)
set @sql = ‘Select column1, column2, column3 from ‘ + @tablename + ‘ order by column1′
exec (@sql)

Note ( ) around @sql

Discuss This Question:  

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following