Good evening (or good morning) friends all around the globe! Wherever you are, whatever you do, wishing you a nice rest of the day (or night!) 🙂
This is the last of a series of posts that deal with the task of importing and exporting unstructured data in SQL Server.
The first article of this series, explained how you can store and export binary files in earlier versions of SQL Server such as SQL Server 2005 with the use of the image datatype.
The second article presented the FILESTREAM technology which was first introduced in SQL Server 2008.
This article discusses the FileTables feature which builds on top of SQL Server FILESTREAM technology. FileTables was first introduced in SQL Server 2012.
What is FileTables in SQL Server?
The FileTables feature allows the user to store unstructured data (i.e. files, documents, images, etc.) in special tables in SQL Server called FileTables, but being able to access them from the file system. To this end, if you have an application that needs to access unstructured data, you can directly access them from the File System even though the data is stored into FileTables in SQL Server.
Example of Using SQL Server FileTables
Enough with the talking, let’s see an example of using this great feature.
Enable FileStram
First, let’s enable FILESTREAM (this is a prerequisite – for more info, see the second article of this series):
Figure 1: Enable FILESTREAM.
Then, let’s create a FILESTREAM-enabled database (make sure to update the path “C:\BlogSQLData” if you are using a different one):
CREATE DATABASE FileStreamDB
ON
PRIMARY ( NAME = FileStreamDBData, FILENAME = 'C:BlogSQLDatafilestreamDB_data.mdf'),
FILEGROUP FileStreamGroup_1 CONTAINS FILESTREAM( NAME = FileStreamDBFS,
FILENAME = 'C:BlogSQLDatafilestream1')
LOG ON ( NAME = FileStreamDBLogs, FILENAME = 'C:BlogSQLDatafilestreamDB_log.ldf');
GO
Then we need to enable non-transactional access at the database level as well as specify the directory for FileTables:
ALTER DATABASE FileStreamDB
SET FILESTREAM ( NON_TRANSACTED_ACCESS = FULL, DIRECTORY_NAME = N'FileTablesDir')
GO
Now it’s time to create the FileTable:
USE FileStreamDB;
GO
CREATE TABLE DocumentStore AS FileTable
WITH (
FileTable_Directory = 'FileTablesDir',
FileTable_Collate_Filename = database_default
);
GO
Check out FileTables
OK, it’s time for the magic to take place. Let’s check the contents of the FileTable:
Figure 2: The FileTable in SSMS Object Explorer.
As you can see, the FileTable “DocumentStore” is currently empty:
Figure 3: Checking the contents of the FileTable.
Now, let’s open the FileTable directory in Explorer, and by accessing it directly from the Windows File System, copy some files:
Figure 4: Access the FileTable directory in Explorer.
Figure 5: Copy files in the FileTable directory.
Now, let’s check again the contents of the FileTable from SSMS:
Figure 6: Checking the FileTable contents after copying the files from Windows Explorer.
And that’s it! As you can see in the above screenshot, by copying the two files from Windows Explorer directly into the FileTable directory, the corresponding two records have been created in the FileTable in the SQL Server instance! Now you can access the files either from the SQL Server Database Engine via T-SQL or directly from the Windows File System!
FileTables along with the FILESTREAM technology, enables the user with more options when it comes to storing binary objects in SQL Server. The seamless integration of FileTables with the Windows File System (NTFS) allows the user to store large binary objects in fast speeds and at the same time to be able to use the powerful features of SQL Server’s Database Engine to traverse this data (i.e. full-text search, semantic search, etc.).
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!)
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:7,467
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.