Question

How to insert the resultset given by the commands

RESTORE FILELISTONLY
RESTORE  HEADERONLY
RESTORE VERIFYONLY

into an automatically generated temp table ?

I would like to use a technique similar to (so the table is auto created, with all the columns matching the resultset's columns)

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

But this doesn't work. If I could populate a TempTable I could then be able to use the information contained in it in a following SQL Statement (in my case a restore DB statement in which I need to use some strings contained in the resultset given by RESTORE FILELISTONLY)

I am using sql server 2008.

Was it helpful?

Solution

Personally, this is one scenario where I would avoid pure TSQL and use an external script or program. Depending on what you're trying to do, you might find that using Smo from Powershell or .NET completely avoids the need for TSQL anyway. I say that because working with backups always seems to lead to working with files outside the database, and then TSQL is just too awkward.

Having said all that, if you're sure that you must do this in TSQL, then you can do something like this:

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

Or to be a bit nicer:

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

You would still have to create the table first, though, which is no big deal and makes sense anyway if you do this a lot. Books Online lists the data type for each column in the result set, but at least for me (SQL2008 SP1) the documentation does not match the actual result set so you might need to tweak it.

OTHER TIPS

I know the OP was using 2008, but we've all moved on a few years and I just wrote a stored proc for 2014 which picks out the DatabaseBackupLSN, so thought I would share...

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

The RESTORE HEADERONLY docs are here.

Example usage:

DECLARE @databaseBackupLsn NUMERIC(25, 0) 

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

SELECT @databaseBackupLsn

Update 18/02/2016: SQL Server 2014 Service Pack 1 added 3 new columns to the RESTORE HEADERONLY output: KeyAlgorithm EncryptorThumbprint EncryptorType. I have added these columns to the procedure above.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top