Solved

How to change a filename during a swl on the fly.

Posted on 2007-11-20
2
275 Views
Last Modified: 2010-03-19
Some Background:
My company has production servers and on a daily basis we download a database to our lan so we can run reports from it. The code we use to load the database is as follows:

USE master
RESTORE DATABASE cheshire_Reporting
FROM DISK = 'C:\BAckup\cheshire\cheshire_backup_200711190030.bak'
WITH  REPLACE
, MOVE 'cheshire_Data' TO 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\cheshire_reporting_Data.mdf'
, MOVE 'cheshire_Log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\cheshire_reporting_Log.ldf'
, MOVE 'sysft_ft_cheshire' TO 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\sysft_ft_cheshire_reporting'
GO
I created a sql jog with this code and it works with no problems.

Problem:
My production server is set up to save the .bak file as the filename_Backup_Date.bak.
For ex. - cheshire_backup_200711190030.bak
This date changes everyday

Question - Is there a way to create a sql server job that I can set up to run daily that will take this name
change into consideration??

thanks
John

0
Comment
Question by:hexvader
2 Comments
 
LVL 25

Accepted Solution

by:
imitchie earned 250 total points
ID: 20324142
DECLARE @place varchar(max)
set @place = 'C:\BAckup\cheshire\cheshire_backup_' + replace(replace(replace(convert(varchar(16), getdate(), 20),'-', ''),' ',''),':','') + '.bak'
RESTORE DATABASE cheshire_Reporting
FROM DISK = @place
0
 

Author Comment

by:hexvader
ID: 20327807
We had to manipulate the code slighly to deal with the date actually being 1 day behind the current date but it worked like a charm.
Thanks and happy Thanks!
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

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.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

770 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