Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7854137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:57:21+00:00 2026-06-02T19:57:21+00:00

What is the fastest way to export files (blobs) stored in a SQL Server

  • 0

What is the fastest way to export files (blobs) stored in a SQL Server table into a file on the hard drive? I have over 2.5 TB of files (90 kb avg) stored as varbinary and I need to extract each one to a local hard drive as quickly as possible. BCP seems to work but it will take over 45 days with the speed I’m seeing, and I’m worried that my script will fail at some point because Management Studio will run out of memory.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T19:57:24+00:00Added an answer on June 2, 2026 at 7:57 pm

    I tried using a CLR function and it was more than twice as fast as BCP. Here’s my code.

    Original Method:

    SET @bcpCommand = 'bcp "SELECT blobcolumn FROM blobtable WHERE ID = ' + CAST(@FileID AS VARCHAR(20)) + '" queryout "' + @FileName + '" -T -c'
    EXEC master..xp_cmdshell @bcpCommand
    

    CLR Method:

    declare @file varbinary(max) = (select blobcolumn from blobtable WHERE ID = @fileid)
    declare @filepath nvarchar(4000) = N'c:\temp\' + @FileName
    SELECT Master.dbo.WriteToFile(@file, @filepath, 0)
    

    C# Code for the CLR function

    using System;
    using System.Data;
    using System.Data.SqlTypes;
    using System.IO;
    using Microsoft.SqlServer.Server;
    
    namespace BlobExport
    {
        public class Functions
        {
          [SqlFunction]
          public static SqlString WriteToFile(SqlBytes binary, SqlString path, SqlBoolean append)
          {        
            try
            {
              if (!binary.IsNull && !path.IsNull && !append.IsNull)
              {         
                var dir = Path.GetDirectoryName(path.Value);           
                if (!Directory.Exists(dir))              
                  Directory.CreateDirectory(dir);            
                  using (var fs = new FileStream(path.Value, append ? FileMode.Append : FileMode.OpenOrCreate))
                {
                    byte[] byteArr = binary.Value;
                    for (int i = 0; i < byteArr.Length; i++)
                    {
                        fs.WriteByte(byteArr[i]);
                    };
                }
                return "SUCCESS";
              }
              else
                 "NULL INPUT";
            }
            catch (Exception ex)
            {          
              return ex.Message;
            }
          }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a 1GB sql text file I'm importing into MySQL. I've used a
what's the probably fastest way of reading relatively huge files with Java's I/O-methods? My
What is the fastest way to count lines and words in a text file
What is the fastest way of getting a file length in .net? Note: I
What is the fastest way to convert a String into an array of Short
what would be the fastest way to merge a list of numpy arrays into
What's the fastest way to check if my server is online via JavaScript? I've
what's the fastest way of getting a variable into a given range? For example,
What is the fastest way to check if a file contains a certain string
What is the absolute fastest way to read and write strings from a file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.