Wednesday 4 November 2020

Export Documents saved as Blob / Binary from SQL Server

 https://www.jitendrazaa.com/blog/sql/sqlserver/export-documents-saved-as-blob-binary-from-sql-server/

Export Documents saved as Blob / Binary from SQL Server

T-SQL Scripts to Export Blob or Binary data stored in SQL Server

I tried many ways to export files or documents saved as binary (Blob) datatypes in SQL Server. However, finally came up with below solution which worked very well. Below script was used to export around 25GB of files stored in SQL Server.

To understand this, lets create a table in Database which will store files from local system into SQL Server as binary / Blob .

1CREATE TABLE [dbo].[Document](
2    [Doc_Num] [numeric](18, 0) IDENTITY(1,1) NOT NULL,
3    [Extension] [varchar](50) NULL,
4    [FileName] [varchar](200) NULL,
5    [Doc_Content] [varbinary](max) NULL
6) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

How to Insert Blob into Database

For demo purpose, we will save two files in our table using below queries

1INSERT [dbo].[Document] ([Extension] ,[FileName] , [Doc_Content] )
2SELECT 'pdf', 'Salesforce Lightning.pdf',[Doc_Data].*
3FROM OPENROWSET
4    (BULK 'C:\G2\My POC\Blog\SQL Server\Source\lightning.pdf', SINGLE_BLOB)  [Doc_Data]
5 
6INSERT [dbo].[Document] ([Extension] ,[FileName] , [Doc_Content] )
7SELECT 'html', 'Progress.html',[Doc_Data].*
8FROM OPENROWSET
9    (BULK 'C:\G2\My POC\Blog\SQL Server\Source\Progress.html', SINGLE_BLOB)  [Doc_Data]

If we try to see content in actual table, it will look like

Insert Blob into Database
Insert Blob into Database

Export Blob From SQL Server and save it as a file

For demo purpose, we want to save documents on local disc. We will use Doc_Num to be created as folder and document will be saved in that folder. To create folders using SQL Server, we will use stored procedure CreateFolder created in this post.
Note : We will create only one folder per document. If multiple nested folder needs to be created then we need to iterate through each folder and call CreateFolder stored procedure for each folder. Its explained in this blog post.

Now time to see actual T-SQL which will iterate through all documents, create folder and save Blob as a file on local disc.

1USE [POC]
2DECLARE @outPutPath varchar(50) = 'C:\G2\My POC\Blog\SQL Server\Extract Blob'
3, @i bigint
4, @init int
5, @data varbinary(max)
6, @fPath varchar(max
7, @folderPath  varchar(max)
8  
9--Get Data into temp Table variable so that we can iterate over it
10DECLARE @Doctable TABLE (id int identity(1,1), [Doc_Num]  varchar(100) , [FileName]  varchar(100), [Doc_Content] varBinary(max) )
11  
12INSERT INTO @Doctable([Doc_Num] , [FileName],[Doc_Content])
13Select [Doc_Num] , [FileName],[Doc_Content] FROM  [dbo].[Document]
14  
15--SELECT * FROM @table
16 
17SELECT @i = COUNT(1) FROM @Doctable
18  
19WHILE @i >= 1
20BEGIN
21 
22    SELECT
23     @data = [Doc_Content],
24     @fPath = @outPutPath + '\'+ [Doc_Num] + '\' +[FileName],
25     @folderPath = @outPutPath + '\'+ [Doc_Num]
26    FROM @Doctable WHERE id = @i
27  
28  --Create folder first
29  EXEC  [dbo].[CreateFolder]  @folderPath
30   
31  EXEC sp_OACreate 'ADODB.Stream', @init OUTPUT; -- An instace created
32  EXEC sp_OASetProperty @init, 'Type', 1; 
33  EXEC sp_OAMethod @init, 'Open'; -- Calling a method
34  EXEC sp_OAMethod @init, 'Write', NULL, @data; -- Calling a method
35  EXEC sp_OAMethod @init, 'SaveToFile', NULL, @fPath, 2; -- Calling a method
36  EXEC sp_OAMethod @init, 'Close'; -- Calling a method
37  EXEC sp_OADestroy @init; -- Closed the resources
38  
39  print 'Document Generated at - '+  @fPath  
40 
41--Reset the variables for next use
42SELECT @data = NULL 
43, @init = NULL
44, @fPath = NULL 
45, @folderPath = NULL
46SET @i -= 1
47END

Variable @outPutPath stores root folder path, where folders will be created and Blob content would be exported into it as a file.

Below image shows output in action :

SQL Server Export Blob as File
SQL Server Export Blob as File

No comments:

Post a Comment

Note: only a member of this blog may post a comment.

Blog Archive