Solved

Create new table with date at end of table name

Posted on 2009-04-03
2
249 Views
Last Modified: 2012-05-06
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?

0
Comment
Question by:srejja
  • 2
2 Comments
 
LVL 14

Expert Comment

by:Christopher Gordon
ID: 24064173
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
0
 
LVL 14

Accepted Solution

by:
Christopher Gordon earned 500 total points
ID: 24064186
this assumes the first step in your proess is to create a table called myTable_Temp.  The last stop would be to drop the table.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question