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 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:
|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|
Now let’s store these two files in a SQL Server table.
–Create test database
CREATE DATABASE [BinaryFilesDB];
|Figure 4: Contents of the table after importing the binary files.|
|Figure 5: Renamed contents of table “tblFiles”|
‘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
|Figure 6: Generated statements for exporting the binary files.|
Now let’s run the two bcp commands on the command prompt:
|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:
- DBA Security Advisor: Secure your SQL Server instances against security risks.
- In-Memory OLTP Simulator: Easily benchmark SQL Server’s In-Memory OLTP Engine against your custom workload.
- Artemiou SQL Books: Download my latest free books on SQL Server.
- Artemiou Data Tools: See my latest software projects.
Recommended eBooks on SQL Server: