In my program I want to insert the images of students which are in a folder to the sql table according to id. Currently I used this querry.
declare EmployeCursor cursor fast_forward for
select num
from login
declare @sql nvarchar(4000)
declare @num nvarchar(50)
open EmployeCursor
while (1=1) begin
fetch next from EmployeCursor into @num
if @@FETCH_STATUS<>0 break
set @sql = N'UPDATE login
SET image =
(SELECT * FROM
OPENROWSET(BULK N''D:\img\' + cast(@num as nvarchar(50)) + N'.jpg'', SINGLE_BLOB) AS img)
WHERE num = ' + cast(@num as nvarchar(max))
exec(@sql)
end /* while */
close EmployeCursor
deallocate EmployeCursor
But its have problem.My image names are in the format 12-1.jpg and id in the format 12/1. so how can I add images? pls help
vishnu sureshPosted Jan 5, 2015, 9:02 AM
Khargesh RajputPosted Jan 5, 2015, 6:20 AM
and 30 rows are updated becauses you may update with where cluase.
vishnu sureshPosted Jan 5, 2015, 4:54 AM
vishnu sureshPosted Jan 5, 2015, 4:53 AM
DECLARE image_cursors CURSOR FOR
SELECT CODE FROM MyMast WHERE img IS NULL
OPEN image_cursors;
FETCH NEXT FROM image_cursors
INTO @id;
WHILE @@FETCH_STATUS = 0
BEGIN
DECLARE @sql VARCHAR(MAX)
DECLARE @imagePath VARCHAR(255)
SET @imagePath = 'D:\images' + RTRIM(LTRIM(@id)) + '.jpg'
SET @sql = 'UPDATE Mymast'
SET @sql = @sql + 'SET img = (SELECT BulkColumn FROM OPENROWSET( BULK ''' + @imagePath + ''', Single_Blob) AS Picture), SET PictureFileName = ' + @imagepath
SET @sql = @sql + 'WHERE id = ''' + @id + ''';'
BEGIN TRY
EXECUTE sp_executesql @sql
END TRY
BEGIN CATCH
END CATCH
FETCH NEXT FROM image_cursors
INTO @id;
END
CLOSE image_cursors;
DEALLOCATE image_cursors;
SELECT id, img FROM MyMast WHERE img IS NOT NULL
vishnu sureshPosted Jan 5, 2015, 4:50 AM
Khargesh RajputPosted Jan 5, 2015, 3:34 AM
vishnu sureshPosted Jan 5, 2015, 1:26 AM
sorry sir, I have a lot of collection of stud id, so it cannot be changed. is there any other coding available to insert 111-1.jpg to 111/1 id place,
eg: code:
replacing / with - for id
Khargesh RajputPosted Jan 5, 2015, 12:46 AM
change it to 111-1
vishnu sureshPosted Jan 5, 2015, 12:35 AM
vishnu sureshPosted Jan 5, 2015, 12:32 AM
Khargesh RajputPosted Jan 4, 2015, 11:39 PM
vishnu sureshPosted Jan 3, 2015, 9:10 AM
Khargesh RajputPosted Jan 3, 2015, 5:57 AM
e.g. save any file with name abc/.jpg not accept