October 19, 2007 at 8:37 am
Is there a way of saving binary data, retrieved from the database, to disk using just TSQL?
Thanks...
October 19, 2007 at 8:55 am
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# :).
If that is not an option, have a look at http://www.sqlservercentral.com/articles/Miscellaneous/writingtoafileusingthesp_oacreatestoredprocedurean/1694/
Regards,
Andras
October 19, 2007 at 9:06 am
Thanks for the info Andras.
Interesting article.
October 20, 2007 at 7:05 pm
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# :).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
Change is inevitable... Change for the better is not.
October 21, 2007 at 12:13 pm
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?
October 21, 2007 at 12:45 pm
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
Change is inevitable... Change for the better is not.
October 22, 2007 at 12:48 am
I have used BCP to archive binary data to disk. You need to specify a format file with no format for this to work.
October 22, 2007 at 1:19 am
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
October 22, 2007 at 1:46 am
Thanks Martin & Nebjosa,
Looks exactly what I need. I will try it.
Ta
October 22, 2007 at 6:59 am
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?
October 22, 2007 at 7:42 am
To be honest to you I'm using this script for pdf files and I haven't test it for jpg. I'll do some test tomorrow if it's not too late to you.
October 22, 2007 at 7:53 am
The BCP method with a cursor and [server] command shell appears to be quite kludgy. First off, the extraction of the BLOB is written to the SQL Server server when you may wish to extract from a client machine. Never mind the security issues with invoking the command shell inside the server in a production environment. Cursors are not a good thing, and lastly, the constant context switching to the command shell to extract each record is a performance issue.
So an application is the proper choice. A quick program solution is to use VBScript as per the attached sample (just rename the extension from "txt" to "vbs").
October 22, 2007 at 11:56 pm
Now you know why I don't let images and other blobs into may databases... too much of a hassle. I just store the names of files in the database and let the GUI guys pluck the images.
--Jeff Moden
Change is inevitable... Change for the better is not.
October 23, 2007 at 12:07 am
try out this link with following samples.
October 23, 2007 at 12:24 am
In general I will agree with John and Jeff. But sometimes it's not just like that.
For example, having images on file system is good solution but then backup becomes an open issue. Security too. Who will do it, where, how you will do restore,...
... or ... you should avoid using cursor and bcp moving extract process to the client. It could be good approach but how will you automate this procedure in reliable way.
There are many questions you'd answer before you choose the solution.
Viewing 15 posts - 1 through 15 (of 23 total)
You must be logged in to reply to this topic. Login to reply