我有一个TSQL脚本,看起来它应该从表中检索图像数据,然后将其导出到在脚本开头声明的起始文件夹下的文件夹中。没有错误,脚本看起来应该可以工作。我怀疑我没有发现一个小毛病,但是欢迎任何想法
--Declares
DECLARE @ImageData VARBINARY (max);
DECLARE @Path2OutFile NVARCHAR (2000);
DECLARE @Obj INT;
DECLARE @ImageFolderPath NVARCHAR(1000);
DECLARE @ImageFilename NVARCHAR(1000);
SET @ImageFolderPath = '\\Test01\Export';
Declare @ImageFolder NVARCHAR(1000);
SET @ImageFolder = '';
Declare @TestOutFolder as NVARCHAR(1000);
Declare @Folder_Exists as INTl
Declare @file_results table
(file_exists int,
file_is_a_directory int,
parent_directory_exists int
);
SET NOCOUNT ON
DECLARE db_cursor CURSOR FOR
SELECT ProductImageFile, ProductImageFolder,convert(varbinary(max),ImageData,1)
from TempT2
OPEN db_cursor
FETCH NEXT FROM db_cursor
INTO @ImageFilename, @ImageFolder, @ImageData
WHILE @@FETCH_STATUS = 0
BEGIN
--Test if folder exists
set @TestOutFolder = CONCAT (@ImageFolderPath,'\', @ImageFolder);
insert into @file_results
(file_exists, file_is_a_directory, parent_directory_exists)
exec master.dbo.xp_fileexist @TestOutFolder
select @Folder_Exists = file_is_a_directory
from @file_results
if @Folder_Exists = 0 --Folder does not exist. Create new folder and output to it. Otherwise skip
Begin
--If Exists Output
SET @Path2OutFile = CONCAT (
@ImageFolderPath
,'\'
, @ImageFolder
,'\'
, @ImageFilename
+'.Jpeg'
);
BEGIN TRY
EXEC sp_OACreate 'ADODB.Stream' ,@Obj OUTPUT;
EXEC sp_OASetProperty @Obj ,'Type',1;
EXEC sp_OAMethod @Obj,'Open';
EXEC sp_OAMethod @Obj,'CreateFolder',@TestOutFolder, OUT, @TestOutFolder;
EXEC sp_OAMethod @Obj,'Write', NULL, @ImageData;
EXEC sp_OAMethod @Obj,'SaveToFile', NULL, @Path2OutFile, 2;
EXEC sp_OAMethod @Obj,'Close';
EXEC sp_OADestroy @Obj;
END TRY
BEGIN CATCH
EXEC sp_OADestroy @Obj;
END CATCH
End
--Clean Up Table Folder for next iteration
DELETE from @file_results
--Fetch Next Data From Cursor
FETCH NEXT FROM db_cursor
INTO @ImageFilename, @ImageFolder, @ImageData
END
CLOSE db_cursor
DEALLOCATE db_cursor
没有错误消息。
运行调试会检索数据并执行所需的所有步骤。