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!

Table Name from a string 1

Status
Not open for further replies.

Smithsc

MIS
Apr 20, 2007
143
GB
Is it possible to create a table where the table name comes from a string. For example:

DECLARE @tbname varchar(30)
SET @tbname = 'tbl_' & YEAR(GetDate()) & '_Postings'

SELECT
Col1 as C1
Col2 as C2
INTO
@tbname
FROM
dbo.Table
 
You just need to make it dynamic.

DECLARE @tbname varchar(30)
SET @tbname = 'tbl_' + cast(YEAR(GetDate()) as varchar(4)) + '_Postings'

declare @test varchar(200)

print @tbname

set @test = 'SELECT id as c1, name as c2 INTO ' + @tbname + ' FROM fruit'

print @test

exec (@test)


Simi
 
also....

DECLARE @tbname varchar(30)
SET @tbname = 'tbl_' + cast(YEAR(GetDate()) as varchar(4)) + '_Postings'

print @tbname

exec ('SELECT id as c1, name as c2 INTO ' + @tbname + ' FROM fruit')

Simi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top