Export Multiple Images

CODE TO EXPORT MULTIPLE IMAGES AT ONCE

CREATE PROCEDURE dbo.ExportImage (
 @ImgName NVARCHAR (100)
,@ImageFolderPath NVARCHAR(1000)
,@Filename NVARCHAR(1000)
)
AS
BEGIN

   DECLARE @ImageData VARBINARY (max);
   DECLARE @Path2OutFile NVARCHAR (2000);
   DECLARE @Obj INT

   SET NOCOUNT ON

DECLARE @cursor CURSOR
SET @cursor = CURSOR FOR
    SELECT convert (VARBINARY (max), ImgData, 1)
     FROM Images
     WHERE TicketID = @ImgName
OPEN @cursor
FETCH NEXT
FROM @cursor INTO @ImageData
WHILE @@FETCH_STATUS = 0
BEGIN
   SET @Path2OutFile = CONCAT (
     @ImageFolderPath
     ,'\'
     , @Filename
     );
 BEGIN TRY
 EXEC sp_OACreate 'ADODB.Stream' ,@Obj OUTPUT;
 EXEC sp_OASetProperty @Obj ,'Type',1;
 EXEC sp_OAMethod @Obj,'Open';
 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

FETCH NEXT
FROM @cursor INTO @ImageData
END
CLOSE @cursor
DEALLOCATE @cursor

LEAVE A REPLY

Please enter your comment!
Please enter your name here