Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Create new table with date at end of table name

Posted on 2009-04-03
2
Medium Priority
?
263 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

885 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