Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Save Image to Disk in T-SQL

Posted on 2011-02-18
4
Medium Priority
?
1,288 Views
Last Modified: 2012-05-11
Hello Experts,

We need to extract a BLOB from our SQL Server database, and then write that image to disc. The image is a Microsoft Word doc. We think we're almost there but are stuck on one last thing.

When the file is actually written to disk, using the ADODB.stream object, only a small fraction of the file is written to disk (about 1 KB). The file/image is verified to be in the column/field in its entirety,  as we can successfully extract it using our client via ADO.GetChunk methods.

Any ideas as to the cause?

Thanks,
Bleary-eyed
DECLARE @mytextptr varbinary(16), @totalsize int,
     @lastread int, @readsize int,@AutoID int,@ObjectToken INT

SET NOCOUNT ON
SET @AutoID =1103 --limit to the record of interest

SELECT
    @mytextptr=TEXTPTR(Attachments), @totalsize=DATALENGTH(Attachments),
     @lastread=0,
     @readsize=CASE WHEN (@@TEXTSIZE < DATALENGTH(Attachments)) THEN
         @@TEXTSIZE ELSE DATALENGTH(Attachments) END
      FROM tblInsertedObjects WHERE AutoID=@AutoID
 
-- loop through as needed to populate @mytextptr
IF @mytextptr IS NOT NULL AND @readsize > 0
     WHILE (@lastread < @totalsize)
     BEGIN
         READTEXT tblInsertedObjects.Attachments @mytextptr @lastread @readsize
         IF (@@error <> 0)
             BREAK    
         SELECT @lastread=@lastread + @readsize
         IF ((@readsize + @lastread) > @totalsize)
             SELECT @readsize=@totalsize - @lastread
     END

-- Save @mytextptr to disk
EXEC sp_OACreate 'ADODB.Stream', @ObjectToken OUTPUT
EXEC sp_OASetProperty @ObjectToken, 'Type', 1
EXEC sp_OAMethod @ObjectToken, 'Open'
EXEC sp_OAMethod @ObjectToken, 'Write', NULL, @mytextptr
EXEC sp_OAMethod @ObjectToken, 'SaveToFile', NULL, 'd:\This_is_the_output.doc',2
EXEC sp_OAMethod @ObjectToken, 'Close'
EXEC sp_OADestroy @ObjectToken

Open in new window

0
Comment
Question by:Bleary-Eyed
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 15

Expert Comment

by:Aaron Shilo
ID: 34931826
0
 

Author Comment

by:Bleary-Eyed
ID: 34933080
Hi ashilo,

Thanks for your response. However, we need assiatance with this in T-SQL.
0
 

Accepted Solution

by:
Bleary-Eyed earned 0 total points
ID: 34938350
We figured it out. there was no need for the READTEXT. We just placed the BLOB field into the myTestPtr variable in the Select statement and then used the -- ---Save @mytextptr to disk-- code to save it to disk. Worked great.
0
 

Author Closing Comment

by:Bleary-Eyed
ID: 34978014
We figured it out on our own
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

604 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