Importing and exporting unstructured data from a database is a common practice, especially in large applications. SQL Server 2008 introduced the
feature that allows storing unstructured data (i.e. music, video, documents, etc.) onto the NTFS file system and manipulating it via the Database Engine. SQL Server 2012 introduced
which is an enhancement to FILESTREAM.
With this post I am starting a series of articles that will deal with the topic of storing, manipulating and extracting unstructured data from SQL Server.
In this article we are going to see how it is possible to import and export binary objects in
. Instead of saying more I would just like to show you by example how you can do this.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiv6VfElVMSGt7u5wHfkgNgCh4s8zBtscCPyn1oKIihFB0rhxyQEaljGz0imuaivRjJ8LKnK7J4nGe75RYwYkjsSlZ2avGFzOvSx7l8iKQFpLEe02ngZPEFUuJ4LJqrqYy4nqmQLZKiI2r1/s1600/Blobs_Part1_Sc1.png) |
Figure 1: The two sample files for the BLOBs example. |
Let's take a closer look at the files just for checking out their content:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjoL5Xi2Net04N_6l-09i5dRlfy2qSII7H05ZfD76GIGtiVpilU0Tj8B9NkIJrA_eRKCXTMwULazRwVNfAlJ2bgx_yiHMa9x_FHpGHDba19WJdujJkfJRJTfHgXdUutwsvT82OyS0wLzAHW/s1600/Blobs_Part1_Sc2.png) |
Figure 2: Content of SampleTextFile.txt |
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhGorrdv78zfhcZhvHPY-GHinW0xqLr0nvS0sOAskq5_0E5DJgaawrA6hNHUAjgbzPyyXn3jUtOUwHVWxK4bAbvjEqNOKVm8H1yj11n675BUe43hvnfu3_bPZVSpKjkFvf91qAsU98ercYU/s1600/Blobs_Part1_Sc3.png) |
Figure 3: Content of SampleImage.png |
Now let's store these two files in a SQL Server table.
USE master;
GO
--Create test database
CREATE DATABASE [BinaryFilesDB];
GO
--Use test database
USE [BinaryFilesDB];
GO
--Create table that will be hosting the files
CREATE TABLE [dbo].[tblFiles](
[fileID] [bigint] IDENTITY(1,1),
[fileName] [nvarchar](255) NULL,
[binFile] [image] NOT NULL);
GO
--
--Import SampleTextFile.txt
--
INSERT INTO dbo.tblFiles
([fileName],[binFile])
VALUES ('SampleTextFile.txt',(SELECT * FROM OPENROWSET(BULK 'c:\testing\SampleTextFile.txt',SINGLE_BLOB) AS x))
GO
--
--Import SampleImage.png
--
INSERT INTO dbo.tblFiles
([fileName],[binFile])
VALUES ('SampleImage.png',(SELECT * FROM OPENROWSET(BULK 'c:\testing\SampleImage.png',SINGLE_BLOB) AS x))
GO
Let's check the contents of "tblFiles" table:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgzs6lK1XV4oUHpTle9sWmK_9v8aluNO5jUuSfLkWE1C5dZiokIggDBqmOv7ciPPcU5j9PUcMPLfqHoGY6wkIkkvVuluAPsVfsiFDxdXFdtYSM4tuB_VxKTBlLWGvUhuUUleUJjsXvgS-wg/s1600/Blobs_Part1_Sc4.png) |
Figure 4: Contents of the table after importing the binary files. |
Let's rename the files, just for performing a minor modification:
--Rename files
UPDATE tblFiles
SET fileName='SampleTextFileModified.txt'
WHERE filename='SampleTextFile.txt';
GO
UPDATE tblFiles
SET fileName='SampleImageModified.png'
WHERE filename='SampleImage.png';
GO
Let's check the table contents again:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhr9tygh3C6Oi-OwXnk9VtQmezKe8aAlP1ZDEmVfoc2uEK8cZ4OfCw2hftW6n2JCkj_MueW_PthbTihoZniZLWf0BPzlTaGQ6_ieWJITp4tVDtsu71Y_jL3mk4JJIBFmPYDdvnsw7jAUGCi/s1600/Blobs_Part1_Sc5.png) |
Figure 5: Renamed contents of table "tblFiles" |
Now let's produce the export statements:
SELECT
'bcp "select binFile from BinaryFilesDB.dbo.tblFiles where fileid=' + cast (fileID as varchar(50)) + '" queryout "c:\testing\'+[filename]
+'" -f bcp.fmt -S .\SQL2K14CTP2 -T' as RunTheseOnCommandPrompt
FROM BinaryFilesDB.dbo.tblFiles;
GO
Some notes on the above T-SQL statement:
- SQL2K14CTP2 is the named instance of my SQL Server
- Also note that I am using Trusted (-T) connection for my generated bcp commands
- If you want to use username/password instead of a Trusted connection replace "-T" with -U [username] -P [password]
- You can download the bcp.fmt format file from
here.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjM87oJ4_6DhbWKBWzWb0UOLuPkkuqJ5FYePG1xDhteeCDzVYBd0NmQdnQolC7DGFFNRPE-lTJL694vDTwDWzwsiH2usJt8p8ldb4H4lEoULYgfzFnX1bzSGuvvV62o0IYFOipJRP5GTxt4/s1600/Blobs_Part1_Sc6.png) |
Figure 6: Generated statements for exporting the binary files. |
Now let's run the two bcp commands on the command prompt:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgsfMbYaOegF0Kj9-THMmLKSi2SHYx-HyVxQ42gMbNz03u_X5fVj8PUdNewxTvK-RKwVyBe1PvEE2ZpPcEP6XX-21gIO_IsXA6zNLbcfFYPy-Zj_OzYGZDxaJQOEqW_oLyPzdWQ1U-sbF7S/s1600/Blobs_Part1_Sc7.png) |
Figure 7: Executing the bcp commands for exporting the binary files. |
Also, let's check the contents of the "c:\testing" directory:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhbEIGSpct6zT0mF5gIzWAEehSFVHmOa330pBH5VXO5GhboCH47qjVLnLhV6JlmvIr9a6E703jxrVx9K2joAUk_myt8zeB-z4W_5uox9NVQO6POhWk6vnCkEU9dgffmhkd4_0uvR-ybkCrP/s1600/Blobs_Part1_Sc8.png) |
Figure 8: Binary files successfully exported from SQL Server. |
As you can see, the two renamed files were successfully exported and have the exact same size as the original ones!
I hope you enjoyed the article!
Until next time!
--
My Latest Projects: