How to insert information provided by RESTORE FILELISTONLY / HEADERONLY / VERIFYONLY into a temporary table

How to insert a result set specified by commands

RESTORE FILELISTONLY
RESTORE  HEADERONLY
RESTORE VERIFYONLY

to automatically generated temp table?

I would like to use a technique similar to (so that the table is created automatically, with all columns matching the columns of the result set)

SELECT * INTO #TempTable 
FROM (RESTORE FILELISTONLY FROM DISK = 'c:\Test\Test.bak')

But that does not work. If I could fill out TempTable, I could use the information contained in it in the following SQL statement (in my case, a DB recovery statement in which I need to use some of the rows contained in the result set provided by RESTORE FILELISTONLY)

I am using SQL Server 2008.

+5
2

, TSQL script . , , , Smo Powershell .NET TSQL. , , , , , TSQL .

, , TSQL, - :

insert into dbo.BackupFiles (LogicalName, PhysicalName, ...)
exec('RESTORE FILELISTONLY FROM DISK = ''c:\Test\Test.bak''')

:

declare @Command nvarchar(4000)
-- you can build the command string some other way, of course
set @Command = N'RESTORE FILELISTONLY FROM DISK = ''c:\Test\Test.bak'''

insert into dbo.BackupFiles (LogicalName, PhysicalName, ...)
exec sp_executesql @Command

, , . Books Online , , , (SQL2008 SP1) , .

+7

, OP 2008, , 2014, DatabaseBackupLSN, , ...

CREATE PROCEDURE [Utilities].[GetDatabaseBackupLsn]
(
    @filePath VARCHAR(1000),
    @databaseBackupLsn NUMERIC(25, 0) OUT
)
AS
BEGIN

DECLARE @backupInfo TABLE
(
    BackupName nvarchar(128),
    BackupDescription nvarchar(255),
    BackupType smallint,
    ExpirationDate datetime,
    Compressed bit,
    Position smallint,
    DeviceType tinyint, 
    UserName nvarchar(128),
    ServerName nvarchar(128),
    DatabaseName nvarchar(128),
    DatabaseVersion int,
    DatabaseCreationDate datetime,
    BackupSize numeric(20, 0),
    FirstLSN numeric(25, 0),
    LastLSN numeric(25, 0),
    CheckpointLSN numeric(25, 0),
    DatabaseBackupLSN numeric(25, 0),
    BackupStartDate datetime,
    BackupFinishDate datetime,
    SortOrder smallint,
    [CodePage] smallint,
    UnicodeLocaleId int,
    UnicodeComparisonStyle int,
    CompatibilityLevel tinyint,
    SoftwareVendorId int,
    SoftwareVersionMajor int,
    SoftwareVersionMinor int,
    SoftwareVersionBuild int,
    MachineName nvarchar(128),
    Flags int,
    BindingId uniqueidentifier,
    RecoveryForkId uniqueidentifier,
    Collation nvarchar(128),
    FamilyGUID uniqueidentifier,
    HasBulkLoggedData bit,
    IsSnapshot bit,
    IsReadOnly bit,
    IsSingleUser bit,
    HasBackupChecksums bit,
    IsDamaged bit,
    BeginsLogChain bit,
    HasIncompleteMetaData bit,
    IsForceOffline bit,
    IsCopyOnly bit,
    FirstRecoveryForkID uniqueidentifier,
    ForkPointLSN numeric(25, 0),
    RecoveryModel nvarchar(60),
    DifferentialBaseLSN numeric(25, 0),
    DifferentialBaseGUID uniqueidentifier,
    BackupTypeDescription nvarchar(60),
    BackupSetGUID uniqueidentifier,
    CompressedBackupSize bigint,
    Containment tinyint,
    KeyAlgorithm nvarchar(32),
    EncryptorThumbprint varbinary(20),
    EncryptorType nvarchar(32)
)

DECLARE @sql NVARCHAR(1100)
SET @sql = N'RESTORE HEADERONLY FROM DISK = ''' + @filePath + ''''

INSERT @backupInfo
EXEC(@sql)

SELECT @databaseBackupLSN = DatabaseBackupLSN 
FROM @backupInfo

END

RESTORE HEADERONLY .

:

DECLARE @databaseBackupLsn NUMERIC(25, 0) 

EXEC GetDatabaseBackupLsn 
    'd:\transfer\YourDatabaseBackup_2015_07_09_05_31_59.bak', 
    @databaseBackupLsn OUT

SELECT @databaseBackupLsn

18/02/2016: SQL Server 2014 1 (SP1) 3 RESTORE HEADERONLY output: KeyAlgorithm EncryptorThumbprint EncryptorType. .

+4

All Articles