We are SQL Server Experts. Call Us TODAY! +17738095456 or email us at sales@itimpact.com
  • The Team
  • Our Services
    • Dashboards Developers
    • Microsoft SQL Server
    • Microsoft SQL Server hosting for Access
    • SQL Server Integration Services (SSIS)
    • SQL Server Reporting Services (SSRS)
    • SQL Server Data Quality Services (DQS)
    • Microsoft Access
    • Microsoft VB.Net
    • Cross Functional / Business Unit Integration
    • Financial Statement Consolidation
    • Website Design
  • Case Studies
    • Automatic Reporting for Security Services Firm
    • Amazon API Integration with Access
    • Custom Quoting and Proposal Solution
    • Data Quality Analysis for Government Program
    • New Claim System Reduces Processing Time
    • Pipe Tracking Software
    • Product Search Analysis
    • Safety Training, Incident and Accident Tracking Tool
    • Union Dues and Membership Management System
  • Blog
  • Contact
  • The Team
  • Our Services
    • Dashboards Developers
    • Microsoft SQL Server
    • Microsoft SQL Server hosting for Access
    • SQL Server Integration Services (SSIS)
    • SQL Server Reporting Services (SSRS)
    • SQL Server Data Quality Services (DQS)
    • Microsoft Access
    • Microsoft VB.Net
    • Cross Functional / Business Unit Integration
    • Financial Statement Consolidation
    • Website Design
  • Case Studies
    • Automatic Reporting for Security Services Firm
    • Amazon API Integration with Access
    • Custom Quoting and Proposal Solution
    • Data Quality Analysis for Government Program
    • New Claim System Reduces Processing Time
    • Pipe Tracking Software
    • Product Search Analysis
    • Safety Training, Incident and Accident Tracking Tool
    • Union Dues and Membership Management System
  • Blog
  • Contact
Toggle panel Social icons
Blog

Two Ways of Placing varbinary(max) Data into Files

In this article I will show two ways of placing into files data that is stored in varbinary(max) columns.  It is typical to have an application provide the interface through which to get files in and out of the database.  However, what if you want to extract a bunch of those files to a folder?  The first method uses FileTables; the second uses the Export Column transformation in SSIS.

Method 1: Using FileTables

New in SQL Server 2012, and building on FILESTREAM technology, FileTables allow manipulating files through both the file system and with TSQL in a manner transparent to the other.  Previously with FILESTREAM, the files where stored in a regular Windows folder but they could only be operated on with a special API in order to maintain the synchronization with the database.  For example, with FileTables you may add a file to a database table by dropping it in its designated Windows folder or by using TSQL DML statements.  Either way, the file appears to be both in the folder and in the table but it is really physically stored only in the folder.  To configure FileTables, please refer to this MSDN article.

The strategy is to move the varbinary(max) contents of the existing table to a FileTable, and then the corresponding files will appear in the folder associated with the FileTable.

The first step is to create the FileTable, like this:

CREATE TABLE MyFileTable AS FILETABLE
WITH
(
FILETABLE_DIRECTORY = 'MyExportedFiles'
)
GO

The FILETABLE_DIRECTORY indicates the subfolder in which the files associated with MyFileTable will be stored.  By the way, to quickly navigate to this subfolder, right-click on the newly-created table and select Explore FileTable Directory.

Now simply insert the data into the filetable.  For example, there are two tables in the AdventureWorks2012 database that store varbinary(max) data.  These two INSERT statements do the trick:

insert into [dbo].[MyFileTable] ([file_stream],[name])
select [Document], [FileName] from [Production].[Document] where [Document] is not null;

insert into [dbo].[MyFileTable] ([file_stream],[name])
select [LargePhoto], [LargePhotoFileName] from [AdventureWorks2012].[Production].[ProductPhoto];

After this, the subfolder will the populated with about 110 files:

MyFiles

Method 2: Using the Export Column Transformation in SSIS

The second method is to export the data to files with an SSIS package, using the Export Column transformation.  In this transformation, the basic setup is to indicate the column with the data to export and the column with the full name for the file.

I get the data from the two tables by using two OLE DB source transformations and then add a FilePath column to provide the path and name for the output file.  The resulting data flow looks like this:

DataFlow

By the way, both methods work similarly well with image columns.

No Comments on “Two Ways of Placing varbinary(max) Data into Files”

  1. Juan Soto October 28, 2014 at 5:19 pm

    Thanks Adolfo for a great article!

    Reply

Click here to cancel reply.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

XHTML: You can use these tags <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Recent Posts

  • 23 Jan. 2019 7 essential SQL skills that can help you command a higher paycheck

  • 18 Jan. 2018 Quick Guide on Importing Data from SQL Server (or SQL Azure) to Cosmos DB

  • 10 Jan. 2018 Top 7 Questions about Basic Availability Groups

  • 04 Jan. 2018 An introduction to mssql-cli, a command-line client for SQL Server

  • Share and work together on the intranet with SharePoint Team Sites

ITImpact

About IT Impact, Inc.

IT Impact, Inc.

IT Impact, Inc. is a minority-owned software company specializing in custom software solutions. We work with SQL Server, Microsoft Access, web applications, and much more.

Our staff is top of the line and includes two Microsoft MVPs!

Twitter

Tweets by @ITImpactInc

Facebook

© ITImpact. Webdesign by GreatCustomWebsites.com