我正在尝试将 BLOB 提取到相关目录中的文件中。 这是我的桌子:
执行代码后,它应该在@outPutPath 中为表中的每一行创建所有 3 个文件夹,并在其中创建文件。
我有用于提取文件的工作代码(代码下方),但是当我添加两行额外的行(在@fPath 中添加@folderName)以创建目录时,它不再工作了。可能是目录检查是否存在并创建将是一个好主意,不确定。如果有人可以帮忙吗?
sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'Ole Automation Procedures', 1;
GO
RECONFIGURE;
GO
--------------------------------------------------------------------------------------
drop table [dbo].[Document];
CREATE TABLE [dbo].[Document](
[Doc_Num] [numeric](18, 0) IDENTITY(1,1) NOT NULL,
[Folder_Name] [varchar](50) NULL,
[Extension] [varchar](50) NULL,
[FileName] [varchar](200) NULL,
[Doc_Content] [varbinary](max) NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
----------------------------------------------------------------------------------
SET IDENTITY_INSERT [Document] ON
GO
--------------------------------------------------------------------------------------
delete from [dbo].[Document] where Doc_Num in (1,3,4);
Insert into Document (Doc_Num,Folder_Name,Extension,FileName,Doc_Content)
select 2 as Doc_Num,'Test2','JPG' AS Extension,'TestImage.jpg' as FileName,*
from openrowset(bulk 'C:\temp\BICH\TestBLOB\Import\TestImage.jpg', SINGLE_BLOB)
as X
GO
Insert into Document (Doc_Num,Folder_Name,Extension,FileName,Doc_Content)
select 5 as Doc_Num,'Test5','JPG' AS Extension,'Angle2.png' as FileName,*
from openrowset(bulk 'C:\temp\BICH\TestBLOB\Import\Angle2.png', SINGLE_BLOB)
as X
GO
Insert into Document (Doc_Num,Folder_Name,Extension,FileName,Doc_Content)
select 6 as Doc_Num,'Test6','JPG' AS Extension,'iLoveTrD.png' as FileName,*
from openrowset(bulk 'C:\temp\BICH\TestBLOB\Import\iLoveTrD.png', SINGLE_BLOB)
as X
GO
-- select * from [Document]
-------------------------------------------------------------------
DECLARE @outPutPath varchar(50) = 'C:\temp\BICH\TestBLOB\Export'
, @i bigint
, @init int
, @data varbinary(max)
, @fPath varchar(max)
, @folderPath varchar(max)
, @folderName 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) , [Folder_Name] varchar(100) ,
[FileName] varchar(100), [Doc_Content] varBinary(max) )
INSERT INTO @Doctable([Doc_Num],[Folder_Name],[FileName],[Doc_Content])
Select [Doc_Num],[Folder_Name],[FileName],[Doc_Content] FROM [dbo].[Document]
SELECT @i = COUNT(1) FROM @Doctable
WHILE @i >= 1
BEGIN
SELECT
@data = [Doc_Content],
@folderName = [Folder_Name],
@fPath = @outPutPath + '\' + '\' + [Doc_Num] +'_' + [FileName],
-- @fPath = @outPutPath + '\' + @folderName + '\' + [Doc_Num] +'_' + [FileName],
@folderPath = @outPutPath + '\' + @folderName
FROM @Doctable WHERE id = @i
--EXEC master.dbo.xp_create_subdir @folderPath; -- Creating sub-directory
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