Solved

Create new table with date at end of table name

Posted on 2009-04-03
2
247 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

In this article I will describe the Copy Database Wizard 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.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

760 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now