Hi there,
I was using the code
below code to generate the files from varbinary, it is generating the file but with 0 size
Can anyone help me where I was doing mistake? Appreciate your help
DECLARE @outPutPath varchar(100)
, @i bigint
, @init int
, @data varbinary(max)
, @fPath varchar(max)
, @folderPath varchar(max)
,@sqlcommand varchar(max)
,@targetLocation varchar(max)
--Get Data into temp Table variable so that we can iterate over it
DECLARE @Doctable TABLE (id int identity(1,1), [Doc_Num] varchar(max) , [FileName] varchar(max), [Doc_Content] varBinary(max) )
/****** Script for SelectTopNRows command from SSMS ******/
SELECT
@sqlcommand=[SourceQuery], @outPutPath=[TargetLocation]
FROM [dbo].[FilesMetadata]
where Status ='Y'
INSERT INTO @Doctable([Doc_Num] , [FileName],[Doc_Content])
exec(@sqlCommand)
SELECT @i = COUNT(1) FROM @Doctable
WHILE @i >= 1
BEGIN
SELECT
@data = [Doc_Content],
@fPath = @outPutPath + '\' + [FileName],
@folderPath = @outPutPath --+ '\'+ [Doc_Num]
FROM @Doctable WHERE id = @i
--Create folder first
--EXEC [dbo].[CreateFolder] @folderPath
EXEC sp_OACreate 'ADODB.Stream', @init OUTPUT; -- An instace created
--EXEC sp_OASetProperFolder @init, 'Folderpe', 1;
EXEC sp_OAMethod @init, 'Open'; -- Calling a method
EXEC sp_OAMethod @init, 'Write', NULL, @data; -- Calling a method
EXEC sp_OAMethod @init, 'SaveToFile', NULL, @fPath, 2; -- Calling a method
EXEC sp_OAMethod @init, 'Close'; -- Calling a method
EXEC sp_OADestroy @init; -- Closed the resources
print 'Document Generated at - '+ @fPath
--Reset the variables for next use
SELECT @data = NULL
, @init = NULL
, @fPath = NULL
, @folderPath = NULL
SET @i -= 1
END