从SQL Server导出Blob并将其保存为文件

时间:2018-11-05 21:17:14

标签: sql-server tsql blob ole-automation

我正在尝试将数据从SQL Server导出到文件。数据是docs,pdf,jpegs,xls和sms。我找到了一个可以执行此操作的脚本,但是无论何时运行它,我都会收到一条非描述性的错误消息

C:\ ExtractBlob \ folder1 \ folder2 \ folder3 消息50000,级别16,状态1,过程CreateFolder,第30行[批处理开始第8行]

我的代码如下:

创建用于存储Blob的文件夹的步骤

            sp_configure 'show advanced options', 1;
            GO
            RECONFIGURE;
            GO
            sp_configure 'Ole Automation Procedures', 1;
            GO
            RECONFIGURE;
            GO

            CREATE PROCEDURE [dbo].[CreateFolder] (@newfolder varchar(1000)) AS 
            BEGIN 
            DECLARE @OLEfolder   INT 
            DECLARE @OLEsource   VARCHAR(255) 
            DECLARE @OLEdescription  VARCHAR(255)
            DECLARE @init   INT 
            DECLARE @OLEfilesytemobject INT

            -- it will fail if OLE automation not enabled
            EXEC @init=sp_OACreate 'Scripting.FileSystemObject', @OLEfilesytemobject OUT 
            IF @init <> 0 
            BEGIN 
                EXEC sp_OAGetErrorInfo @OLEfilesytemobject 
                RETURN 
            END 
            -- check if folder exists
            EXEC @init=sp_OAMethod @OLEfilesytemobject, 'FolderExists', @OLEfolder OUT, @newfolder
            -- if folder doesnt exist, create it 
                IF @OLEfolder=0 
                BEGIN 
                EXEC @init=sp_OAMethod @OLEfilesytemobject, 'CreateFolder', @OLEfolder OUT, @newfolder 
            END 
            -- in case of error, raise it  
            IF @init <> 0 
                BEGIN 
                    EXEC sp_OAGetErrorInfo @OLEfilesytemobject, @OLEsource OUT, @OLEdescription OUT 
                    SELECT @OLEdescription='Could not create folder: ' + @OLEdescription 
                    RAISERROR (@OLEdescription, 16, 1)  
                END 
            EXECUTE @init = sp_OADestroy @OLEfilesytemobject 
            END 

导出代码

            USE [IQS]
            DECLARE @outPutPath varchar(50) = 'C:\Users\trenton.gibbs\Documents\Extract'
            , @i bigint
            , @init int
            , @data varbinary(max)
            , @fPath varchar(max) 
            , @folderPath  varchar(max)

            --Get Data into temp Table variable so that we can iterate over it
            DECLARE @Imagetable TABLE (id int identity(1,1), [Doc_Num]  varchar(100) , [FileName]  varchar(100), [Doc_Content] varBinary(max) )

            INSERT INTO @Imagetable([Doc_Num],[FileName],[Doc_Content])
            Select [Link_Embed_Sysid],[File_Location], Convert(varbinary(max),[Embed_Object]) FROM  [dbo].[Link_Embed]
            Where Create_Date Between '9/1/2018' And '9/8/2018'
            And [Embed_Object] IS NOT NULL

            --SELECT * FROM @Imagetable

            SELECT @i = COUNT(1) FROM @Imagetable

            WHILE @i >= 1
            BEGIN

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

              --Create folder first
              EXEC  [dbo].[CreateFolder]  @folderPath

              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

有人知道什么可能导致此错误吗?

1 个答案:

答案 0 :(得分:1)

您可能要检查文件夹创建路径。

@outPutPath varchar(50) = 'C:\Users\trenton.gibbs\Documents\Extract'

它可能与权限相关,或者路径无效。例如,要创建文件夹“ Extract”,[C:\Users\trenton.gibbs\Documents\]必须已经存在。