Solved

Save Image to Disk in T-SQL

Posted on 2011-02-18
4
1,272 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
  • 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

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

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

705 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

15 Experts available now in Live!

Get 1:1 Help Now