5 pts.
 Pass table name to procedure including fields
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

Software/Hardware used:
ASKED: May 14, 2012  5:55 AM
UPDATED: May 16, 2012  9:18 AM

Answer Wiki:
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
Last Wiki Answer Submitted:  May 14, 2012  1:31 pm  by  Smf   170 pts.
All Answer Wiki Contributors:  Smf   170 pts.
To see all answers submitted to the Answer Wiki: View Answer History.


Discuss This Question:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _