How to Import and Export Unstructured Data in SQL Server – FILESTREAM

How to Import and Export Unstructured Data in SQL Server – FILESTREAM

A few weeks ago I posted an article on how to import and export unstructured data in SQL Server 2005 by using the “image” data type.

In this article I will show how you can manipulate unstructured data in SQL Server 2008 or later by using the FILESTREAM feature. FILESTREAM allows storing unstructured data (i.e. music, video, documents, etc.) onto the NTFS file system and manipulating it via the Database Engine.

Step 1: Enabling FILESTREAM
Before using the FILESTREAM feature you have to enable it. To this end you need to navigate to SQL Server Configuration Manager and under SQL Server Services right click on the SQL Server instance, select properties and in the “FileStream” tab check “Enable FILESTREAM for Transact-SQL access” and “Enable FILESTREAM for file I/O access”:

Figure 1: Enabling FILESTREAM

The last step for enabling FILESTREAM is from within SSMS, to open a new query window and execute the following T-SQL statement and then restart the SQL Server instance:

EXEC sp_configure filestream_access_level, 2;

Note: the available filestream access levels are:

  • 0: Disables FILESTREAM support for this instance.
  • 1: Enables FILESTREAM for Transact-SQL access.
  • 2: Enables FILESTREAM for Transact-SQL and Win32 streaming access.

Step 2: Creating a FILESTREAM-Enabled Database 
For creating a FILESTREAM-enabled database you just need to include a FILESTREAM filegroup. For example:

PRIMARY ( NAME = FileStreamDBData,
    FILENAME = ‘C:BlogSQLDatafilestreamDB_data.mdf’),
    FILENAME = ‘C:BlogSQLDatafilestream1’)
LOG ON  ( NAME = FileStreamDBLogs,
    FILENAME = ‘C:BlogSQLDatafilestreamDB_log.ldf’);
Step 3: Creating a Table for Storing FileStream Data
The only difference between a “normal” table and a table that can store filestream data is the use of the “FILESTREAM” data type for a specific column in the table’s definition script:
USE [FileStreamDB];

[Id] [uniqueidentifier] ROWGUIDCOL NOT NULL UNIQUE, 
[FileName] VARCHAR(100),
Step 4: Storing FileStream Data
For this example, consider the following unstructured data file:
Figure 2: Image file to be stored in FILESTREAM-enabled database.

Now, let’s store the file in our FILESTREAM-enabled database and table that was created earlier:

USE [FileStreamDB];

    VALUES (newid (), 
Here’s the contents of the table:
Figure 3: The contents of the FILESTREAM-enabled table after inserting unstructured data (image file).

As you can see, the file is visible on the file system level too:

Figure 4: Binary file stored using FILESTREAM – Accessible on the file system level.

Now let’s try to open the file using MS Paint: 

Figure 5: Accessing the data stored in the FILESTREAM-enabled database from the file system level (Windows).

As you can see, the image file is stored in the SQL Server database table but besides T-SQL access, you can also access it from Windows! 

What we just did with the above example, shows a small glimpse of the real power of FILESTREAM, that is leveraging the performance and rich APIs of the Windows file system and at the same time maintaining consistency between structured and unstructured data in SQL Server.

FILESTREAM actually works like a bridge between structured and unstructured data via a combination of transactional data and file system access and can be extremely useful in cases where you have many binary objects like images and videos and you want to store it in SQL Server and being able to access it with the speed of the file system.

Artemakis Artemiou
Artemakis Artemiou is a Senior SQL Server and Software Architect, Author, and a 9 Times Microsoft Data Platform MVP (2009-2018). He has over 15 years of experience in the IT industry in various roles. Artemakis is the founder of SQLNetHub and Artemakis is the creator of the well-known software tools Snippets Generator and DBA Security Advisor. Also, he is the author of many eBooks on SQL Server. Artemakis currently serves as the President of the Cyprus .NET User Group (CDNUG) and the International .NET Association Country Leader for Cyprus (INETA). Artemakis's official website can be found at

2 thoughts on “How to Import and Export Unstructured Data in SQL Server – FILESTREAM

  1. Nikodem Dobrzański

    OK, I saw an import filestream data, but I didn't see an export filestream data at your post. Title is missing or you forgot add example how to export data from existing table, that used FILESTREAM.

  2. Artemakis Artemiou [MVP]

    Hi Nikodem,

    Thank you for your comment.

    When you use FileStream for storing binary files in SQL Server, you can access these files directly from the File System (see Figure 4) as well because they are actually stored in the File System. The files can be managed from SQL Server or directly from the File System. That's the main benefit of using FileStream.