patternsqlModerate
Extracting a field from RESTORE HEADERONLY
Viewed 0 times
fieldheaderonlyextractingfromrestore
Problem
I'm trying to use 'RESTORE HEADERONLY' to get the date when the backup I'm about to restore was made.
The command:
works fine in Query Analyzer and gives a resultset with something like 50 columns.
The problem is actually accessing this from code.
I can get this into a temp table by declaring every single one of the 50:ish columns, inserting into it with
The problem is that I really want to avoid having to declare the entire resultset as a temp table as it seems like a very brittle solution if they ever add columns to it in future versions.
Is there any way to just get a single column out of this resultset without declaring all the columns?
The command:
RESTORE HEADERONLY FROM DISK = ''works fine in Query Analyzer and gives a resultset with something like 50 columns.
The problem is actually accessing this from code.
I can get this into a temp table by declaring every single one of the 50:ish columns, inserting into it with
exec and getting the value I want from there.The problem is that I really want to avoid having to declare the entire resultset as a temp table as it seems like a very brittle solution if they ever add columns to it in future versions.
Is there any way to just get a single column out of this resultset without declaring all the columns?
Solution
This works for me.
The ad hoc distributed queries Option needs to be enabled. Or if you don't want to do that you can set up a loopback linked server and use that instead.
SELECT BackupStartDate
FROM OPENROWSET('SQLNCLI',
'Server=MARTINPC\MSSQL2008;Trusted_Connection=yes;',
'SET NOCOUNT ON;SET FMTONLY OFF;EXEC(''
RESTORE HEADERONLY
FROM DISK = ''''C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQL2008\MSSQL\Backup\DB1.bak''''
'')'
)The ad hoc distributed queries Option needs to be enabled. Or if you don't want to do that you can set up a loopback linked server and use that instead.
EXEC sp_addlinkedserver @server = 'LOCALSERVER', @srvproduct = '',
@provider = 'SQLOLEDB', @datasrc = @@servername
SELECT BackupStartDate
FROM OPENQUERY(LOCALSERVER,
'SET FMTONLY OFF;
EXEC(''
RESTORE HEADERONLY
FROM DISK = ''''C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQL2008\MSSQL\Backup\DB1.bak''''
'')')Code Snippets
SELECT BackupStartDate
FROM OPENROWSET('SQLNCLI',
'Server=MARTINPC\MSSQL2008;Trusted_Connection=yes;',
'SET NOCOUNT ON;SET FMTONLY OFF;EXEC(''
RESTORE HEADERONLY
FROM DISK = ''''C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQL2008\MSSQL\Backup\DB1.bak''''
'')'
)EXEC sp_addlinkedserver @server = 'LOCALSERVER', @srvproduct = '',
@provider = 'SQLOLEDB', @datasrc = @@servername
SELECT BackupStartDate
FROM OPENQUERY(LOCALSERVER,
'SET FMTONLY OFF;
EXEC(''
RESTORE HEADERONLY
FROM DISK = ''''C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQL2008\MSSQL\Backup\DB1.bak''''
'')')Context
StackExchange Database Administrators Q#12437, answer score: 12
Revisions (0)
No revisions yet.