DECLARE @Path VARCHAR(1000)= N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Backup\aaa.bak',
@RestorePath NVARCHAR(max)='C:\'
DECLARE @Table TABLE
(
LogicalName VARCHAR(128) ,
[PhysicalName] VARCHAR(128) ,
[Type] VARCHAR ,
[FileGroupName] VARCHAR(128) ,
[Size] VARCHAR(128) ,
[MaxSize] VARCHAR(128) ,
[FileId] VARCHAR(128) ,
[CreateLSN] VARCHAR(128) ,
[DropLSN] VARCHAR(128) ,
[UniqueId] VARCHAR(128) ,
[ReadOnlyLSN] VARCHAR(128) ,
[ReadWriteLSN] VARCHAR(128) ,
[BackupSizeInBytes] VARCHAR(128) ,
[SourceBlockSize] VARCHAR(128) ,
[FileGroupId] VARCHAR(128) ,
[LogGroupGUID] VARCHAR(128) ,
[DifferentialBaseLSN] VARCHAR(128) ,
[DifferentialBaseGUID] VARCHAR(128) ,
[IsReadOnly] VARCHAR(128) ,
[IsPresent] VARCHAR(128) ,
[TDEThumbprint] VARCHAR(128)
)
DECLARE @LogicalNameData VARCHAR(128) ,
@LogicalNameLog VARCHAR(128)
INSERT INTO @table
EXEC ('
RESTORE FILELISTONLY
FROM DISK=''' + @Path + '''
'
)
DECLARE @restoreScript NVARCHAR(max)='RESTORE DATABASE [aaa] FROM DISK =''' + @Path + ''' WITH FILE = 1 '
SELECT @restoreScript +=CHAR(10) + ' ,MOVE ''' + LogicalName + ''' TO ''' +
@RestorePath + LogicalName + RIGHT(PhysicalName,4) + ''''
FROM @Table
WHERE Type = 'D'
SELECT @restoreScript += ' ,MOVE ''' + LogicalName + ''' TO ''' + @RestorePath + LogicalName + '.ldf'''
FROM @Table
WHERE Type = 'L'
SET @restoreScript += ' , NOUNLOAD, REPLACE, STATS = 10 '
SELECT @restoreScript
RESTORE FILELISTONLY的定義經常發生變化,打破了這個腳本。有沒有辦法做一個「SELECT INTO」或「DECLARE CURSOR FOR」? – Brain2000