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

variables in a table name

Status
Not open for further replies.

thminer

Technical User
Oct 11, 2005
16
US
i am trying to figure out if naming a table with a declared variable is possible. i have a stored procedure that runs one time a week. when i run the sproc, i enter a variable known as @weeknumber. i would like the result set to write to a table with the name of the @weeknumber variable. for example, if i declare @weeknumber = 37, i would like the results written to tblweek37. is this possible?
thanks
 
yes use dynamic SQL

declare @FilterString int
set @FilterString = 37

declare @sql varchar (500)
select @sql ='insert into Table' + convert(varchar,@FilterString) + ' select * from temptable'

print (@sql) --testing
exec (@sql)




Denis The SQL Menace
SQL blog:
Personal Blog:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top