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
Let’s take a closer look at the files just for checking out their content:
Strengthen you SQL Server Development Skills – Enroll to our Online Course!
Check our online course titled “Essential SQL Server Development Tips for SQL Developers”
(special limited-time discount included in link).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!
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
--Rename files UPDATE tblFiles SET fileName='SampleTextFileModified.txt' WHERE filename='SampleTextFile.txt'; GO UPDATE tblFiles SET fileName='SampleImageModified.png' WHERE filename='SampleImage.png'; GO
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
Now let’s run the two bcp commands on the command prompt:
Also, let’s check the contents of the "c:\testing"
directory:
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!
Featured Online Courses:
- Boost SQL Server Database Performance with In-Memory OLTP
- Essential SQL Server Administration Tips
- SQL Server Fundamentals – SQL Database for Beginners
- Essential SQL Server Development Tips for SQL Developers
- The Philosophy and Fundamentals of Computer Programming
- .NET Programming for Beginners – Windows Forms with C#
- Introduction to SQL Server Machine Learning Services
- Introduction to Azure SQL Database for Beginners
- SQL Server 2019: What’s New – New and Enhanced Features
- Entity Framework: Getting Started – Complete Beginners Guide
- How to Import and Export Data in SQL Server Databases
- Learn How to Install and Start Using SQL Server in 30 Mins
- A Guide on How to Start and Monetize a Successful Blog
Related SQL Server Administration Articles:
- Essential SQL Sever Administration Tips
- How to Patch a Standalone SQL Server Instance
- The SQL Server Browser Service and UDP Port 1434
- The Maximum Number of Concurrent Connections Setting in SQL Server
- Top 10 SQL Server DBA Daily Tasks List
- There is no SQL Server Failover Cluster Available to Join
- Encrypting a SQL Server Database Backup
- …more
Rate this article:
Reference: SQLNetHub.com (https://www.sqlnethub.com)
© SQLNetHub
Artemakis Artemiou, a distinguished Senior Database and Software Architect, brings over 20 years of expertise to the IT industry. A Certified Database, Cloud, and AI professional, he earned the Microsoft Data Platform MVP title for nine consecutive years (2009-2018). As the founder of SQLNetHub and GnoelixiAI Hub, Artemakis is dedicated to sharing his knowledge and democratizing education on various fields such as: Databases, Cloud, AI, and Software Development. His commitment to simplicity and knowledge sharing defines his impactful presence in the tech community.