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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing a table name into a stored procedure as a variable

Status
Not open for further replies.

EBrownSQL

Programmer
Aug 6, 2002
4
US
I have to populate tables monthly for a printing job. Each table is populated using the same code. Is there a way to pass the table name into a stored procedure and have it populated. When I try to just pass it into the stored procedure I get an error, because the code does not recognize the variable name as a table. I would appreciate any suggestions.
 
Hi,

u will have to create a dynamic SQL query to this
something like this...

create proc procname
@tablename varchar(20)
AS
Declare @SQL varchar(200)
Set @SQL = 'Insert into ' + @tablename + ' values(a,b,c,d)'
exec(@SQL)

Sunil
 
The reason dynamic SQL is needed is because SQL cannot use variables for object or column names. This is stated explicitly in SQL books online. Terry L. Broadbent - DBA
Computing Links:
faq183-874 contains "Suggestions for Getting Quick and Appropriate Answers" to your questions.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top