Solved

T-SQL writing a blob to an external file

Posted on 2009-07-07
5
928 Views
Last Modified: 2012-05-07
Having just figured out how to use OPENROWSET(BULK '...', SINGLE_BLOB) to read binary data from the file system into my SQL Server database, I now need to do the opposite:

Given an "image" type column in a table, I need T-SQL script to write this to a file somewhere on the file system. Again, ideally as NATIVE as possible.

(dang - why are examples of this so hard to find?)

So imagine your select statement to get the blob is

SELECT [data] from [dbo].[blobtable] where [id] = 5

the [data] happens to represent a JPG, so what I need is a wee bit o' T-SQL code that will write [data] out of the above select to 'C:\test.jpg'

many thanks
0
Comment
Question by:WernerVonBraun
  • 3
5 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
there are some methods:
http://www.sqlservercentral.com/articles/Miscellaneous/writingtoafileusingthesp_oacreatestoredprocedurean/1694/

note: the methods all write TEXT, however the ones with vbscript or sp_oaxxxx methods should be easily modifiable to write binary data.
0
 
LVL 4

Author Comment

by:WernerVonBraun
Comment Utility
:-(

I'm not sure it would be easy to modify them to write binary data. The only way I can imagine that would work is by using an ADODB.Stream - however in that case I would at some stage need to store the data locally in a variable of a binary type, and SQL Server dinnae like that?
0
 
LVL 31

Accepted Solution

by:
RiteshShah earned 500 total points
Comment Utility
look at example in below link, there is a way to store image file in table (in blog article) and retrieve it back to file system in SQL Server itself (in comment given by the reader of blog).


http://blog.sqlauthority.com/2009/03/06/sqlauthority-news-author-visit-toronto-canada-insert-image-in-database/

0
 
LVL 4

Author Comment

by:WernerVonBraun
Comment Utility
damn. It involves invoking an external .exe but at least it's "native":

bcp "SELECT BULKCOLUMN from OpenRowSet(BULK 'somefile', SINGLE_BLOB) DMSData" queryout yetanotherfile -U someusername -P itspassword -n
0
 
LVL 4

Author Comment

by:WernerVonBraun
Comment Utility
obviously instead of the OpenRowSet I used above you can select a blob from another table
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video discusses moving either the default database or any database to a new volume.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now