Importing photographs

From MyWiki
Jump to: navigation, search
USE [test]
GO
/****** Object:  StoredProcedure [dbo].[sptest2]    Script Date: 09/03/2017 09:31:48 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE  [dbo].[sptest2] AS
 
SELECT Id
INTO #ControlTable
FROM dbo.ImageTable
 
 
 DECLARE @TableID VARCHAR (30);
 DECLARE @photoid VARCHAR(10);
 DECLARE @photostr VARCHAR(40);
 DECLARE @RESULT INT;
 
 -- set @photoid=(select Name from ImageTable where id='1');
 -- set @photostr= CONCAT('C:\IMSpics14_15\',@photoid,'.jpg');
 -- print @photostr
 
 
 while EXISTS (SELECT * FROM #ControlTable)
BEGIN
 
    SELECT @TableID = (SELECT top 1 Id
                       FROM #ControlTable
                                           ORDER BY Id ASC
                       )
 
    -- Do something with your TableID
        --- print @TableID;
         SET @photostr= CONCAT('C:\IMSpics14_15\',@Tableid,'.jpg');
         print @photostr;
         DECLARE @PATH varchar(20);
 
EXEC master.dbo.xp_fileexist @photostr, @result OUTPUT
 
if(@result=1)
BEGIN
         BEGIN TRY
        update ImageTable set photo=
(SELECT BulkColumn
FROM Openrowset( Bulk 'C:\IMSpics14_15\33105810.jpg', Single_Blob) as image)
 where id=@TableID;
 END TRY
 BEGIN CATCH
 END CATCH
 END
 else
 print 'notfound';
    delete #ControlTable
    where Id = @TableID
 
end
 
 
 drop Table #ControlTable