1

我正在尝试将数据从 SQL Server 导出到文件。数据是 docs、pdf、jpegs、xls 和 sms。我找到了一个可以这样做的脚本,但是每当我运行它时,我都会收到一条非描述性错误消息

C:\ExtractBlob\folder1\folder2\folder3 Msg 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

有谁知道可能导致此错误的原因是什么?

4

1 回答 1

1

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

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

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

于 2018-11-06T16:05:08.613 回答