Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
davidhills
Starting Member
14 Posts |
Posted - 2009-01-20 : 08:05:36
|
Good MorningI have a sql server maintance plan that creates a full backup every day and calls the bak fileLive_YYYYMMDDHHMM.bakwhere yyyy =2009mm=01dd=19hh=03mm=05for exampleThe problem I face is that I want to automatically restore the backup into another sql server and I dont know how to find the backup file name that I want.(it always the latest one available-- this might help!)My restore sql looks like thisUSE masterGOSELECT GETDATE()GOALTER DATABASE [ROV_Live]SET SINGLE_USERWITH ROLLBACK IMMEDIATEGORESTORE DATABASE [ROV_Live] **************problem here how do i fill in the correct yyyymmddhhmm valueFROM DISK = '\\nas\atlantis$\Rov_Live_Kent\Live_yyyymmddhhmm.bak'WITH FILE = 1, MOVE 'ROV_Live' TO 'E:\Data\ROV_Live.mdf', MOVE 'ROV_Live_log' TO 'E:\Data\ROV_Live_log.LDF', NOUNLOAD, REPLACE, STATS = 10GOALTER DATABASE [ROV_Live]SET MULTI_USERGOSELECT GETDATE()GO-- ThanksDavid Hills Was this post |
|
davidhills
Starting Member
14 Posts |
Posted - 2009-01-21 : 06:12:53
|
David, Perhaps you can do something like this: DECLARE @FileName NVARCHAR(255) CREATE TABLE #TempDirectory (BackupFile NVARCHAR(255)) INSERT INTO #TempDirectory EXECUTE master..xp_cmdshell 'dir \\nas\atlantis$\Rov_Live_Kent\Live_*.bak /B' SELECT TOP 1 @FileName = '\\nas\atlantis$\Rov_Live_Kent\' + BackupFile FROM #TempDirectory WHERE filenm LIKE '%.bak' ORDER BY RIGHT(BackupFile,16) DESC DROP TABLE #TempDirectory RESTORE DATABASE [ROV_Live] FROM DISK = @FileName ... RLF |
 |
|
|
|
|