Hi Folks! Few days back i was working on a project, I was trying to automate few things, I will share most of my work in next blogs, so i have also automate restore process for database having multiple files. It will work well with any number of files. You need to provide database name and backup location.
This script is for
Sql Server.
SET NOCOUNT ON
DECLARE @restoreTable TABLE (
[LogicalName] NVARCHAR(128)
,[PhysicalName] NVARCHAR(260)
,[Type] CHAR(1)
,[FileGroupName] NVARCHAR(128)
,[Size] NUMERIC(20, 0)
,[MaxSize] NUMERIC(20, 0)
,[FileID] BIGINT
,[CreateLSN] NUMERIC(25, 0)
,[DropLSN] NUMERIC(25, 0)
,[UniqueID] UNIQUEIDENTIFIER
,[ReadOnlyLSN] NUMERIC(25, 0)
,[ReadWriteLSN] NUMERIC(25, 0)
,[BackupSizeInBytes] BIGINT
,[SourceBlockSize] INT
,[FileGroupID] INT
,[LogGroupGUID] UNIQUEIDENTIFIER
,[DifferentialBaseLSN] NUMERIC(25, 0)
,[DifferentialBaseGUID] UNIQUEIDENTIFIER
,[IsReadOnly] BIT
,[IsPresent] BIT
,[TDEThumbprint] VARBINARY(32)
)
INSERT INTO @restoreTable
EXEC ('RESTORE FILELISTONLY FROM DISK = ''' + @backuppath + '''')
SET @Query = 'RESTORE DATABASE [' + @Dbname + '] FROM DISK = N''' + @backuppath + '''
WITH FILE = 1,'
SELECT @Query = @Query + '
MOVE N''' + LogicalName + ''' TO N''' + PhysicalName + ''','
FROM @restoreTable
SET @Query = @Query + '
NOUNLOAD, REPLACE, STATS = 5'
PRINT @Query