Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing table names as parameters instored procedures? 1

Status
Not open for further replies.

sagn

Programmer
Jun 7, 2001
166
US
Hi,

Is there a way to pass a table name as a parameter
in a stored procedure?

I want to be able to have a general stored procedure
that will perform an operation on a table whose name is passed to the stored procedure as a variable.

I tried to pass
it as a varchar. Then using something like

select * from @tablename, but I get errors
if I try that, It does not seem that the
table is sent as a variable that way. Is there a way
to send a table as a variable?

thanks

sagn
 
Hi Sagn,

You can do it, but then you must put the variable into a dynamic sql statement:


EXEC ('SELECT * FROM ' + @tablename)

That will work correctly....remember to leave a space between FROM and the close quotes though, otherwise SQL server gets very confused.

HTH

Tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top