May 9, 2011 #1 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
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
May 9, 2011 1 #2 simian336 Programmer Sep 16, 2009 723 US 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 Upvote 0 Downvote
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
May 9, 2011 #3 simian336 Programmer Sep 16, 2009 723 US 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 Upvote 0 Downvote
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