SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Saving binay data to disk via TSQL???


Saving binay data to disk via TSQL???

Author
Message
flurk118
flurk118
Old Hand
Old Hand (307 reputation)Old Hand (307 reputation)Old Hand (307 reputation)Old Hand (307 reputation)Old Hand (307 reputation)Old Hand (307 reputation)Old Hand (307 reputation)Old Hand (307 reputation)

Group: General Forum Members
Points: 307 Visits: 222
Is there a way of saving binary data, retrieved from the database, to disk using just TSQL?
Thanks...
Andras Belokosztolszki
Andras Belokosztolszki
SSCertifiable
SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)

Group: General Forum Members
Points: 5143 Visits: 1585
Writing binary data, especially if you want to write out to several files, several objects, ... to disk is a bit of a pain. Your best bet would be to write a small application say in C# Smile.
If that is not an option, have a look at http://www.sqlservercentral.com/articles/Miscellaneous/writingtoafileusingthesp_oacreatestoredprocedurean/1694/

Regards,
Andras



Andras Belokosztolszki, MCPD, PhD
GoldenGate Software
flurk118
flurk118
Old Hand
Old Hand (307 reputation)Old Hand (307 reputation)Old Hand (307 reputation)Old Hand (307 reputation)Old Hand (307 reputation)Old Hand (307 reputation)Old Hand (307 reputation)Old Hand (307 reputation)

Group: General Forum Members
Points: 307 Visits: 222
Thanks for the info Andras.

Interesting article.
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (203K reputation)SSC Guru (203K reputation)SSC Guru (203K reputation)SSC Guru (203K reputation)SSC Guru (203K reputation)SSC Guru (203K reputation)SSC Guru (203K reputation)SSC Guru (203K reputation)

Group: General Forum Members
Points: 203176 Visits: 41947
Andras Belokosztolszki (10/19/2007)
Writing binary data, especially if you want to write out to several files, several objects, ... to disk is a bit of a pain. Your best bet would be to write a small application say in C# Smile.
If that is not an option, have a look at http://www.sqlservercentral.com/articles/Miscellaneous/writingtoafileusingthesp_oacreatestoredprocedurean/1694/

Regards,
Andras


Why aren't you considering BCP with no terminator?

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
flurk118
flurk118
Old Hand
Old Hand (307 reputation)Old Hand (307 reputation)Old Hand (307 reputation)Old Hand (307 reputation)Old Hand (307 reputation)Old Hand (307 reputation)Old Hand (307 reputation)Old Hand (307 reputation)

Group: General Forum Members
Points: 307 Visits: 222
I have a table with many rows, each containing an binary field containing, basically, an image. I would like to query this table and produce a file on disk for each of the rows.

Is this achievable using BCP?
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (203K reputation)SSC Guru (203K reputation)SSC Guru (203K reputation)SSC Guru (203K reputation)SSC Guru (203K reputation)SSC Guru (203K reputation)SSC Guru (203K reputation)SSC Guru (203K reputation)

Group: General Forum Members
Points: 203176 Visits: 41947
Heh... I actually haven't tried it because I won't allow folks to store images in our databases... but I don't see why it wouldn't work... it's just another form of data.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Martin Reyerink-362775
Martin Reyerink-362775
Grasshopper
Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)

Group: General Forum Members
Points: 17 Visits: 14
I have used BCP to archive binary data to disk. You need to specify a format file with no format for this to work.
Nebojsa Ilic
Nebojsa Ilic
SSC-Addicted
SSC-Addicted (467 reputation)SSC-Addicted (467 reputation)SSC-Addicted (467 reputation)SSC-Addicted (467 reputation)SSC-Addicted (467 reputation)SSC-Addicted (467 reputation)SSC-Addicted (467 reputation)SSC-Addicted (467 reputation)

Group: General Forum Members
Points: 467 Visits: 290
Hi, I'm using bcp to queryout images from database and T-SQL code looks something like this:


DECLARE c1 CURSOR FOR
SELECT (KeyField)
FROM (Table)
WHERE ...

OPEN c1

FETCH NEXT FROM c1
INTO @KeyField

WHILE @@FETCH_STATUS = 0
BEGIN

-- Create unique name for the file using KeyField from the table
SELECT @FileName = '(Server path ending with \)' + convert(varchar(18), @KeyField) + '.(ext)'

SET @bcpCommand = 'bcp "SELECT (image) FROM (Table) WHERE KeyField = ' + convert(varchar(18), @KeyField) + '" queryout "'
SET @bcpCommand = @bcpCommand + @FileName + '" -S (ServerName) -T -n'

EXEC master..xp_cmdshell @bcpCommand, no_output

FETCH NEXT FROM c1
INTO @KeyField

END

CLOSE c1
DEALLOCATE c1
flurk118
flurk118
Old Hand
Old Hand (307 reputation)Old Hand (307 reputation)Old Hand (307 reputation)Old Hand (307 reputation)Old Hand (307 reputation)Old Hand (307 reputation)Old Hand (307 reputation)Old Hand (307 reputation)

Group: General Forum Members
Points: 307 Visits: 222
Thanks Martin & Nebjosa,

Looks exactly what I need. I will try it.

Ta
flurk118
flurk118
Old Hand
Old Hand (307 reputation)Old Hand (307 reputation)Old Hand (307 reputation)Old Hand (307 reputation)Old Hand (307 reputation)Old Hand (307 reputation)Old Hand (307 reputation)Old Hand (307 reputation)

Group: General Forum Members
Points: 307 Visits: 222
I have tried the above but have run into an issue.

The table holds an image field containing either tiff, jpg or pdf.
The code works for pdf but the file created for tiff and jpg do not.

Is there a simple reason for this?
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search