SQL SERVER – SSIS and How to Load Binary Large Objects, or Blobs – Notes from the Field #080

[Note from Pinal]: This is an 80th episode of Notes from the Field series. Every time I give an introductory note, however, this time there is no need of intro note. This note is from Andy and as we all know he is amazing person when we have to understand the fundamentals. He has written this blog post with such an interesting way that you must read it to understand the very basic fundamental of SSIS. Let’s learn SSIS and How to Load Binary Large Objects, or Blobs.

SQL SERVER - SSIS and How to Load Binary Large Objects, or Blobs - Notes from the Field #080 andyleonard


I still remember my first experience using SSIS to load binary large objects, or blobs. I was doing a gig for a large retailer, moving data for a new in-store application. Part of the data was product images, mostly 3kb-5kb files. During testing, I noticed the load ran much faster if I did not load the images. I wondered why, so I searched the interwebz for an answer.

I did not find an answer. I found lots of comments and posts telling me I could not, in fact, use SSIS (2005) integrate blob data. At least not the way I was doing it. My response was, “Huh,” as I was already using SSIS to (apparently) do the impossible. I knew right away this represented an opportunity. I learned everything I could about blobs and now, just nine short years later, I’m here to share.

When I searched the interwebz this time, I found an excellent blog post by my friend, John Welch (blog | @john_welch), titled Importing Files Using SSIS. John’s idea is straightforward and very “SSIS-y,” as Kevin Hazzard (blog | @KevinHazzard) says. With John’s permission, I am modeling the example for this post on John’s post.

How Does SQL Server Store Blobs?

Before we dive into a demo project, it’s important to know more about how SQL Server stores blob data. The short answer is: it depends. I can hear you asking, “What does it depend on, Andy?” It depends on the size of the blob data. For larger binary large objects, a pointer to a file location is stored in the row. When the row is read, the pointer points to the file location containing the binary data, and the binary data is streamed to the output.

In this example, we’ll take a look at how we use SSIS to move data from the file system into a SQL Server table. I changed a few things but, again, this example was inspired by John Welch’s post titled Importing Files Using SSIS.

Part 1 – Import Column

The Import Column transformation streams file binaries – the contents of a file – into a binary large object (Blob) “column” in a Data Flow Path. From the Data Flow path, these data can be streamed into a database table Blob field. Let’s demonstrate:

In a default instance of SQL Server 2014, I created a database named ImportPics. Then I created a table named PicFile using this statement:

CREATE TABLE PicFile
(
ID INT IDENTITY(1,1)
, FilePath VARCHAR(255)
, FileContent IMAGE
)

Next I created an SSIS 2014 project named ImportPicFiles and renamed Package.dtsx to ImportPicFiles.dtsx. I added a Data Flow Task and created a package parameter named ImportFilesDir to hold the path to a directory filled with Snagit screenshots:

SQL SERVER - SSIS and How to Load Binary Large Objects, or Blobs - Notes from the Field #080 notes-70-1

I add a script component as a Source adapter, then configure it to consume the $Package::ImportFilesDir package parameter as a ReadOnlyVariable:

SQL SERVER - SSIS and How to Load Binary Large Objects, or Blobs - Notes from the Field #080 notes-70-2

I add an output column named filename (DT_STR, 255):

SQL SERVER - SSIS and How to Load Binary Large Objects, or Blobs - Notes from the Field #080 notes-70-3

In the Script Editor, I add the following code:

using System;
using System.Data;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;
using System.IO;
[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
public class ScriptMain : UserComponent
{
public override void CreateNewOutputRows()
{
DirectoryInfo dir = new DirectoryInfo(Variables.ImportFilesDir.ToString());
foreach (var file in dir.GetFiles())
{
Output0Buffer.AddRow();
Output0Buffer.fileName = file.FullName;
}
Output0Buffer.SetEndOfRowset();
}
}

(Yes, that’s C#. I’m learning new things. Yay me! :{>)

Next, I add and configure an Import Columns transformation. On the Input Columns page, I select the fileName field:
SQL SERVER - SSIS and How to Load Binary Large Objects, or Blobs - Notes from the Field #080 notes-70-4

On the Input and Output Properties tab, I expand the Import Column Output node of the treeview, select Output Columns, and click the Add Column button. I name the column “FileContents” and set the DataType property to DT_IMAGE:

SQL SERVER - SSIS and How to Load Binary Large Objects, or Blobs - Notes from the Field #080 notes-70-5

This next part is a little tricky. You can learn more about configuring this – and other tricky SSIS transformation properties – here.

Select the LineageID property value for the FileContents column and copy it to the clipboard:
SQL SERVER - SSIS and How to Load Binary Large Objects, or Blobs - Notes from the Field #080 notes-70-6

Next, expand the Import Column Input treeview node, then expand Input Columns, and then select the fileName column. In the FileDataColumnID property, paste the value you just copied to the clipboard:

SQL SERVER - SSIS and How to Load Binary Large Objects, or Blobs - Notes from the Field #080 notes-70-7

Add an OLE DB Destination adapter, connect it to the database where you created the dbo.PicFile table earlier, and configure the OLE DB Destination adapter to load dbo.PicFile:

SQL SERVER - SSIS and How to Load Binary Large Objects, or Blobs - Notes from the Field #080 notes-70-8

A successful test execution of the data flow task will appear as shown:

SQL SERVER - SSIS and How to Load Binary Large Objects, or Blobs - Notes from the Field #080 notes-70-9

The Import Column transformation is a powerful to load files into database blob columns.

Read SSIS and Blobs, Part 2 to learn even more.

If you want to get started with SSIS with the help of experts, read more over at Fix Your SQL Server.

Reference: Pinal Dave (https://blog.sqlauthority.com)

C Sharp, Notes from the Field, SQL Server, SSIS
Previous Post
SQL SERVER – PowerShell Script – When Was SQL Server Last Restarted?
Next Post
SQL SERVER – Service Pack Error – The User Data directory in the registry is not valid. Verify DefaultData key under the instance hive points to a valid directory

Related Posts

2 Comments. Leave new

  • Good article, Andy and Pinal.
    At the end, there’s a reference to “Read SSIS and Blobs, Part 2 to learn even more”
    Do you have a link to that Part 2 ?

    Thanks.

    Reply
  • Thanks for the article. There is actually another but similar problem but with Variables of that type (varbinary/image etc). We can store the values in variable of type Object, but then there is a problem to pass the values to the stored procedure as a parameter for example

    Reply

Leave a Reply