I actually have a brand new restore stored procedure that I haven't yet posted to my weblog:SET QUOTED_IDENTIFIER ON GOSET ANSI_NULLS ON GO------------------------------------------------------------------------------------------------------ OBJECT NAME : isp_Restore-- AUTHOR : Tara Duggan-- DATE : November 3, 2005-- INPUTS : @dbName - name to use for the restored database-- @bkpDir - full path to the directory where the BAK files exist-- @newDataLocation - new location and file name for the MDF, can be NULL-- @newLogLocation - new location and file name for the LDF, can be NULL-- OUTPUTS : None-- RETURN CODES : 0 - success-- 1 - invalid path-- 2 - no BAK files exist in @bkpDir-- 3 - backup contains more than 2 D files which isn't supported by this sproc-- 4 - restore error-- DEPENDENCIES : None-- DESCRIPTION : This stored procedure performs a restore of the newest BAK file in a specified directory.-- EXAMPLES (optional) : /* DECLARE @rc tinyint EXEC @rc = dbo.isp_Restore @dbName = 'SomeDB', @bkpDir = 'H:\MSSQL\BACKUP\SomeDB\, @newDataLocation = 'F:\MSSQL\DATA\SomeDB_Data.MDF', @newLogLocation = 'G:\MSSQL\DATA\SomeDB_Log.LDF' PRINT @rc*/----------------------------------------------------------------------------------------------------ALTER PROC dbo.isp_Restore(@dbName sysname, @bkpDir nvarchar(1000), @newDataLocation nvarchar(1000), @newLogLocation nvarchar(1000))ASSET NOCOUNT ONDECLARE @cmd sysname, @bkpFile nvarchar(1000), @sql nvarchar(4000), @logicalDataName nvarchar(128), @logicalLogName nvarchar(128)-- Add a backslash to the end of the path if one doesn't existIF REVERSE(SUBSTRING(@bkpDir, 1, 1)) <> '\' SET @bkpDir = @bkpDir + '\'-- Get files sorted by dateSET @cmd = 'dir ' + @bkpDir + '*.BAK /OD'CREATE TABLE #Dir(DirInfo VARCHAR(7000)) -- Stores the dir resultsCREATE TABLE #BackupFiles(BackupDate varchar(10), BackupFileName nvarchar(1000)) -- Stores only the data we want from the dirINSERT INTO #DirEXEC master.dbo.xp_cmdshell @cmdIF EXISTS (SELECT * FROM #Dir WHERE DirInfo = 'The system cannot find the path specified.') RETURN 1INSERT INTO #BackupFilesSELECT SUBSTRING(DirInfo, 1, 10), SUBSTRING(DirInfo, LEN(DirInfo) - PATINDEX('% %', REVERSE(DirInfo)) + 2, LEN(DirInfo)) FROM #DirWHERE ISDATE(SUBSTRING(DirInfo, 1, 10)) = 1 AND DirInfo NOT LIKE '%<DIR>%'IF @@ROWCOUNT = 0 RETURN 2-- Get the newest fileSELECT TOP 1 @bkpFile = BackupFileNameFROM #BackupFilesORDER BY BackupDate DESCDROP TABLE #Dir, #BackupFilesSET @cmd = @bkpDir + @bkpFile-- Stores RESTORE FILELISTONLY result setCREATE TABLE #RestoreFileListOnly( LogicalName nvarchar(128), PhysicalName nvarchar(260), Type char(1), FileGroupName nvarchar(128), [Size] numeric(20,0), [MaxSize] numeric(20,0) )INSERT INTO #RestoreFileListOnlyEXEC('RESTORE FILELISTONLY FROM DISK = ''' + @cmd + '''')IF @@ROWCOUNT <> 2 RETURN 3SELECT @logicalDataName = LogicalNameFROM #RestoreFileListOnlyWHERE Type = 'D'SELECT @logicalLogName = LogicalNameFROM #RestoreFileListOnlyWHERE Type = 'L'DROP TABLE #RestoreFileListOnlySET @sql = ''SET @sql = @sql + 'RESTORE DATABASE ' + @dbName + CHAR(10)SET @sql = @sql + 'FROM DISK = ''' + @cmd + '''' + CHAR(10)SET @sql = @sql + 'WITH' + CHAR(10)SET @sql = @sql + CHAR(9) + 'REPLACE'IF @newDataLocation IS NOT NULLBEGIN SET @sql = @sql + ',' + CHAR(10) SET @sql = @sql + CHAR(9) + 'MOVE ''' + @logicalDataName + ''' TO ''' + @newDataLocation + ''''ENDIF @newLogLocation IS NOT NULLBEGIN SET @sql = @sql + ',' + CHAR(10) SET @sql = @sql + CHAR(9) + 'MOVE ''' + @logicalLogName + ''' TO ''' + @newLogLocation + ''''ENDPRINT @sqlEXEC(@sql)IF @@ERROR <> 0 RETURN 4ELSE RETURN 0GOSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON GO
Check out the header section for the details of it.Tara Kizer