Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Creating files
Message
From
31/10/2019 15:16:58
 
 
To
All
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Title:
Creating files
Miscellaneous
Thread ID:
01671765
Message ID:
01671765
Views:
61
I have the following procedure to create files from a varchar field, but although it seems to run successfully, it does not produce the files. I hope someone can point me into the right direction what the reason for this issue could be:
RECONFIGURE; 
GO 
sp_configure 'Ole Automation Procedures', 1 
GO 
RECONFIGURE; 
GO 
sp_configure 'show advanced options', 1 
GO 
RECONFIGURE;


DECLARE @outPutPath varchar(50) = 'C:\Files\Export'
, @i bigint
, @init int
, @data varchar(max) 
, @fPath varchar(max) 
, @folderPath  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(100) , [FileName]  varchar(100), [Doc_Content] varchar(max))

INSERT INTO @Doctable([Doc_Num], [FileName], [Doc_Content])
              SELECT Employees.emNumber
              , Employees.emNumber
              , SavedPayslips.saFile
       FROM Employees
       JOIN PayEmps ON PayEmps.paEmKey = Employees.emKey
       JOIN Runs ON Runs.ruKey = PayEmps.paRuKey
       JOIN SavedPayslips ON SavedPayslips.saEmKey = Employees.emKey
              AND SavedPayslips.saRuKey = Runs.ruKey

--SELECT * FROM @table

SELECT @i = COUNT(1) FROM @Doctable

WHILE @i >= 1

BEGIN

SELECT
     @data = [Doc_Content],
     @fPath = @outPutPath + '\'+ RTRIM([Doc_Num]) + '\' + RTRIM([FileName]),
     @folderPath = @outPutPath + '\'+ RTRIM([Doc_Num])
    FROM @Doctable WHERE id = @i

EXEC sp_OACreate 'ADODB.Stream', @init OUTPUT; -- An instace created
EXEC sp_OASetProperty @init, 'Type', 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
Christian Isberner
Software Consultant
Next
Reply
Map
View

Click here to load this message in the networking platform