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 .
1
CREATETABLE[dbo].[Document](
2
[Doc_Num] [numeric](18, 0) IDENTITY(1,1)NOTNULL,
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
If we try to see content in actual table, it will look like
Export Blob From SQL Server and save it as a file
For demo purpose, we want to save documents on local disc. We will useDoc_Numto be created as folder and document will be saved in that folder. To create folders using SQL Server,we will use stored procedureCreateFoldercreated 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 callCreateFolderstored procedure for each folder. Its explainedin 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.
No comments:
Post a Comment
Note: only a member of this blog may post a comment.