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

copy table structure only for new table

Status
Not open for further replies.

influent

Programmer
Jan 11, 2001
131
US
I need to create a new table every week, and name it whatever the date is, e.g. tbl082802. The original table has six columns, and I don't want to type in every column's name and length when I create the new table every week. Is there a quick way to create a new table that copies the structure of an existing table?
 
declare @L_dtoday datetime
declare @l_ctablename char(16)
declare @l_csqlstring char(128)
set @l_dtoday = getdate()
set @l_ctablename = 'tbl'+replace(cast(@l_dtoday as char(12)),'/','')
set @l_ctablename = replace(@l_ctablename,' ','')
print @l_ctablename
set @l_csqlstring = 'select top 100 * into '+@l_ctablename+' from mytemplatetable'
print @l_csqlstring
exec(@l_csqlstring)

HTH,
Phil Hegedusich
Senior Web Developer
IIMAK
 
Er, change top 100 to top 1... and

set @l_cSQLstring = 'DELETE FROM '+@l_ctablename
exec @l_cSQLSTring

Sorry for the omission.

PH
 
Try this.

Declare @sql varchar(200)

Select @sql=
'Select * Into Tbl'+
replace(convert(varchar(8),getdate(),1),'/','')+
' From YourBaseTable Where 1=2'

--Optional SQL statement print
--Print @sql

Exec(@sql) 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