Quantcast
Channel: Welcome To TechBrothersIT
Viewing all articles
Browse latest Browse all 2038

How to save resultset of RESTORE FILELISTONLY in temp table in SQL Server - SQL Server Tutorial

$
0
0

Scenario:

I got this task to read the backup files form a folder then then restore to new location. While was working on this task, needed to read the file names from .bak file. I need to move them to new location.

I can use RESTORE FILELISTONLY to get the information. As I need to save the information into variables and build restore query, I had to read this into a temp table.

Below query can be used to save the result set of RESTORE FILELISTONLY to temp table.


--Drop temp table if exits
IF OBJECT_ID('tempdb..#BackupFiles') ISNOTNULL
DROPTABLE #BackupFiles


Declare @BackupFilePath VARCHAR(500)
SET @BackupFilePath='c:\Backup\ReportServer1_20160413_111603.bak'

--Create Temp Table
Createtable #BackupFiles(
LogicalName VARCHAR(128),
PhysicalName VARCHAR(MAX),
Type CHAR(10),
FileGroupName VARCHAR(100),
Size BigInt,
MaxSize BigInt,
Field Int,
CreateLSN INT,
DropLSN INT,
UniqueId uniqueidentifier,
ReadonlyLSN Int,
ReadWriteLSN Int,
BackupSizeInBytes BigInt,
SourceBlockSize Int,
FileGroupIt bit,
LogGroupGUID uniqueidentifier,
DifferentialBaseLSN Bigint,
DifferentialBaseGUID uniqueidentifier,
IsReadOnly bit,
IsPresent bit,
TDEThumprint uniqueidentifier)

--Insert data from RESTORE FILELISTONLY
insertinto #BackupFiles ([LogicalName]
,[PhysicalName]
,[Type]
,[FileGroupName]
,[Size]
,[MaxSize]
,[Field]
,[CreateLSN]
,[DropLSN]
,[UniqueId]
,[ReadonlyLSN]
,[ReadWriteLSN]
,[BackupSizeInBytes]
,[SourceBlockSize]
,[FileGroupIt]
,[LogGroupGUID]
,[DifferentialBaseLSN]
,[DifferentialBaseGUID]
,[IsReadOnly]
,[IsPresent]
,[TDEThumprint])
EXEC('RESTORE FILELISTONLY FROM DISK = '''+@BackupFilePath+'''')


--Check the temp table if data is inserted
Select * From #BackupFiles


I executed above query on different backup files and works great to get me the result set into temp table from RESTORE FILELISTONLY.

How to Insert SQL Server RESTORE FILELISTONLY Result set into Temp table - SQL Server Tutorial


Viewing all articles
Browse latest Browse all 2038

Latest Images

Trending Articles



Latest Images