Friday, 1 July 2011

How to Export Image column to file on MSSQL

It is not that easy how it looks like:) Used the same approach as it is in the answer below but completed the example.

1. Enable the extended stored procedures:
sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'Ole Automation Procedures', 1;
GO
RECONFIGURE;
GO

2. Use sp_OA stored procedures

DECLARE @objStream INT
DECLARE @imageBinary VARBINARY(MAX)
DECLARE @filePath VARCHAR(8000)

SELECT @imageBinary = img
FROM Images
WHERE ID = 1

SET @filePath = 'c:\img_1.jpg'

EXEC sp_OACreate 'ADODB.Stream', @objStream OUTPUT
EXEC sp_OASetProperty @objStream, 'Type', 1
EXEC sp_OAMethod @objStream, 'Open'
EXEC sp_OAMethod @objStream, 'Write', NULL, @imageBinary
EXEC sp_OAMethod @objStream, 'SaveToFile', NULL,@filePath, 2
EXEC sp_OAMethod @objStream, 'Close'
EXEC sp_OADestroy @objStream 

Resources:
How to export a ms sql image column to a file

No comments: