Link to home
Start Free TrialLog in
Avatar of srejja
srejja

asked on

Create new table with date at end of table name

I have a file that is scheduled to import weekly into a table (dbo.WklyImport) on my SQL 2005 Server.  The file structure is the same each week, the only difference being that the new file has more current data.  In the present process the table, dbo.WklyImport, is dropped prior to importing the refreshed file and is replaced by a table of the same name.  So the process is:

Drop dbo.WklyImport
Import text file into dbo.WklyImport

I would like to add a third step which will back up the table prior to dropping it.  The new process would look like this:

declare @maxDate as string
select @maxDate=max(FileDate) from dbo.WklyImport
select * from dbo.WklyImport into dbo.WklyImport + [datepart(mmm,max(ProcessDate) ) + datepart(dd,max(ProcessDate) ) +datepart(yyyy,max(ProcessDate) )]  -- Note: ProcessDate is a field in the table
drop table dbo.WklyImport
Import flat file into dbo.WklyImport


The part that is problem is creating the new table dbo.WklyImportApr032009 from dbo.WklyImport

This part of the process:

declare @maxDate as string
select @maxDate=max(FileDate) from dbo.WklyImport
select * from dbo.WklyImport into dbo.WklyImport + [datepart(mmm,max(ProcessDate) ) + datepart(dd,max(ProcessDate) ) +datepart(yyyy,max(ProcessDate) )]  

Can someone provide me with code that will accomplish this task?

Avatar of Christopher Gordon
Christopher Gordon
Flag of United States of America image

Take a look at sp_rename...

You can do something like this

declare @myTableName varchar(50)
set @myTableName = 'myTable_' + convert(varchar(10), CONVERT(varchar(10), getdate(), 105))

exec sp_rename 'dbo.myTable_Temp', @myTableName

Make sure you use brackets when calling the results of this table.  You may want to goof around with the date formatting in table name in variable @myTableName
ASKER CERTIFIED SOLUTION
Avatar of Christopher Gordon
Christopher Gordon
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial