I have a MSSQL 2k8 database, in it I have a table of format below.

Employee Number   |   Segment   |   Data (varbinary(8000))
111111            |     1       |    0x01234567...DEF
111111            |     2       |    0x01234567...DEF
111111            |     3       |    0x01234567...DEF

The data (varbinary) column makes up a picture but unfortunately is split in multiple segments by a process I cannot control.

Is there a way to export this data via an SQL script/procedure to a file? I have seem some questions that answer for a varbinary(max) column but I can't for the life of me work out how to stitch these all together into one file.

Note: Some of the files have >500 segments but this procedure will not be occuring exceedingly regularly.

  • I don't have the time to write the T-SQL script for you, but: you'll need to use dynamic SQL (probably using a cursor, and that's forgivable in a special circumstance like this), which will iterate through each row, and if the Employee Number matches, just append to the end of the string. Then once the code is generated for each employee, have that dynamic code execute, to save the file to disk. Saying all that, it might be easier to do it in C# or PowerShell or Python. – Randolph West Jul 6 '17 at 6:35
  • 1
    Thanks Randolph for the suggestion. As an FYI I have since completed this in Powershell. Found it to be the easiest solution. – H. Salem Jul 7 '17 at 3:29
  • glad to be of assistance! – Randolph West Jul 7 '17 at 3:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.