Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Create new table with date at end of table name

Posted on 2009-04-03
2
Medium Priority
?
260 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

719 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