In this article, we will be discussing about, how to import and export unstructured data in SQL Server, using the IMAGE datatype.
Introduction
Importing and exporting unstructured data from a database is a common practice, especially in large applications. SQL Server 2008 introduced the FILESTREAM 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 FileTables 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.
Let’s See a Full Example of Using the Datatype
In this article we are going to see how it is possible to import and export binary objects in SQL Server 2005. Instead of saying more I would just like to show you by example how you can do this.
For this example, consider that we have the following two objects:
SampleImage.png
SampleTextFile.txt
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:
Figure 2: Content of SampleTextFile.txt
Figure 3: Content of SampleImage.png
Strengthen you SQL Server Development Skills – Enroll to our Online Course!
Via the course, you will sharpen your SQL Server database programming skills via a large set of tips on T-SQL and database development techniques. The course, among other, features over than 30 live demonstrations!
(Lifetime Access/ Live Demos / Downloadable Resources and more!)
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:
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:
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]
Figure 6: Generated statements for exporting the binary files.
Now let’s run the two bcp commands on the command prompt:
Figure 7: Executing the bcp commands for exporting the binary files.
Also, let’s check the contents of the "c:\testing" directory:
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!
Artemakis Artemiou is a seasoned Senior Database and AI/Automation Architect with over 20 years of expertise in the IT industry. As a Certified Database, Cloud, and AI professional, he has been recognized as a thought leader, earning the prestigious Microsoft Data Platform MVP title for nine consecutive years (2009-2018). Driven by a passion for simplifying complex topics, Artemakis shares his expertise through articles, online courses, and speaking engagements. He empowers professionals around the globe to excel in Databases, Cloud, AI, Automation, and Software Development. Committed to innovation and education, Artemakis strives to make technology accessible and impactful for everyone.
Views:4,813
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept All”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent. Read More
This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
Cookie
Duration
Description
cookielawinfo-checkbox-analytics
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional
11 months
The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy
11 months
The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.