Extract SQL binary data to a disk file

advertisement
Extract SQL binary data to a disk file
by Nick Jacobs on Dec 15, 2011
This routine will pull the binary data (BLOB) from a SQL table and write the physical file to
disk.
DECLARE @SQLIMG VARCHAR(MAX),
@IMG_PATH VARBINARY(MAX),
@TIMESTAMP VARCHAR(MAX),
@ObjectToken INT,
@filename varchar(500),
@filetype varchar(50),
@filepath varchar(4000)
DECLARE IMGPATH CURSOR FAST_FORWARD FOR
select top 10
documentdata,
identifier,
documentfiletype
from documents
where tablename = 'WebImages'
and identifier > 0
OPEN IMGPATH
FETCH NEXT FROM IMGPATH INTO @IMG_PATH,@filename,@filetype
WHILE @@FETCH_STATUS = 0
BEGIN
--SET @TIMESTAMP = 'd:\' +
replace(replace(replace(replace(convert(varchar,getdate(),121),
'-',''),':',''),'.',''),' ','') + '.jpg'
set @filepath = 'c:\temp\img\' + @filename + @filetype
PRINT @filepath
PRINT @SQLIMG
EXEC sp_OACreate 'ADODB.Stream', @ObjectToken OUTPUT
EXEC sp_OASetProperty @ObjectToken, 'Type', 1
EXEC sp_OAMethod @ObjectToken, 'Open'
EXEC sp_OAMethod @ObjectToken, 'Write', NULL, @IMG_PA
TH
EXEC sp_OAMethod @ObjectToken, 'SaveToFile', NULL, @f
ilepath, 2
EXEC sp_OAMethod @ObjectToken, 'Close'
EXEC sp_OADestroy @ObjectToken
FETCH NEXT FROM IMGPATH INTO @IMG_PATH,@filename,@fil
etype
END
CLOSE IMGPATH
DEALLOCATE IMGPATH
For extracting webimages
DECLARE @SQLIMG VARCHAR(MAX),
@IMG_PATH VARBINARY(MAX),
@TIMESTAMP VARCHAR(MAX),
@ObjectToken INT,
@filename varchar(500),
@filetype varchar(50),
@filepath varchar(4000)
DECLARE
@RootCategoryGUID uniqueidentifier,
@RootCategory bigint,
@RootCategoryId nvarchar(20),
@DefaultPriceListId nvarchar(15),
@DefaultPriceList bigint
SELECT TOP 1
@RootCategoryGUID = ProductCategoryGUID,
@RootCategory = ProductCategory
FROM ProductCategories
WHERE ProductCategoryId = 'TECHSILROOT'
DECLARE IMGPATH CURSOR FAST_FORWARD FOR
WITH ProductChanges(Product,ProductProductCategory)
AS
(
SELECT
PL.Product
,PL.ProductProductCategory
FROM dbo.wfn_WebComProductCategoryList(@RootCategory)
PL
INNER JOIN Products P ON P.Product = PL.Product
WHERE
WebProduct = 1
)
SELECT
(SELECT TOP (1) DocumentData FROM Documents WHERE
TableName = 'WebImages' AND Documents.Identifier = P.Product
AND Documents.DocumentLinked = 0) as 'WebImage'
,P.ProductID
,'.jpg'
FROM ProductChanges C
INNER JOIN Products P ON C.Product = P.Product
--WHERE ProductImage IS NOT NULL
--DECLARE IMGPATH CURSOR FAST_FORWARD FOR
-select top 10
-documentdata,
-identifier,
-documentfiletype
-from documents
-where tablename = 'WebImages'
-and identifier > 0
OPEN IMGPATH
FETCH NEXT FROM IMGPATH INTO @IMG_PATH,@filename,@filetype
WHILE @@FETCH_STATUS = 0
BEGIN
--SET @TIMESTAMP = 'd:\' +
replace(replace(replace(replace(convert(varchar,getdate(),121),
'-',''),':',''),'.',''),' ','') + '.jpg'
set @filepath = 'F:\WinmanV7\Images\techsil\' +
@filename + @filetype
PRINT @filepath
PRINT @SQLIMG
if @IMG_PATH IS NOT NULL
BEGIN
EXEC sp_OACreate 'ADODB.Stream',
@ObjectToken OUTPUT
EXEC sp_OASetProperty @ObjectToken, 'Type',
1
EXEC sp_OAMethod @ObjectToken, 'Open'
EXEC sp_OAMethod @ObjectToken, 'Write',
NULL, @IMG_PATH
EXEC sp_OAMethod @ObjectToken,
'SaveToFile', NULL, @filepath, 2
EXEC sp_OAMethod @ObjectToken, 'Close'
EXEC sp_OADestroy @ObjectToken
END
FETCH NEXT FROM IMGPATH INTO
@IMG_PATH,@filename,@filetype
END
CLOSE IMGPATH
DEALLOCATE IMGPATH
Download