views:

35

answers:

2

Dear All, I have another issue. I have in the SQL table Employees. This table has lot of details but I was missing images or photos.

So i managed to take all pictures for all employees but I have the pictures in the folder. Each picture is named like an Employee_id which matches the record in the table. How do I import images into SQL Employee table to match the name of the picture to Employee_id.

Any ideas?

A: 

You can use OPENROWSET BULK to open an external file as a value for an INSERT or UPDATE.

Here's an example:

UPDATE Employees SET Picture=(SELECT * FROM OPENROWSET(BULK 'c:\temp\1234.jpg', SINGLE_BLOB) as Picture) WHERE Employee_id=1234
Mark Ransom
Thanks, but I would actually need more details how to go about it...
Tony
Is there any way to do it thru stored procedure? So I can use it from the application.
Tony
@Tony, you should be able to pass the employee id and filename as parameters to a stored procedure.
Mark Ransom
Thanks Mark, now big question. How?
Tony
A Mark, thanks, I checked a bit more your code above and I see what you meant, let me try to do that as well...
Tony
Mark, I created the sp for above but what should I put in the 'c:\temp\1234.jpg' as image name.... I declared the Employee_id=@EmployeeID
Tony
+2  A: 

A cursor and a bit of dynamic SQL should do the trick.

declare EmployeeCursor cursor fast_forward for
    select Employee_id
        from Employee

declare @sql nvarchar(4000)
declare @Employee_id int

open EmployeeCursor      

while (1=1) begin
    fetch next from EmployeeCursor into @Employee_id

    if @@FETCH_STATUS<>0 break

    set @sql = N'UPDATE Employee
                     SET ImageColumn = 
                         (SELECT * FROM 
                              OPENROWSET(BULK N''c:\images\' + cast(@Employee_id as nvarchar(10)) + N'.jpg'', SINGLE_BLOB) AS img)
                              WHERE Employee_id = ' + cast(@Employee_id as nvarchar(10))

    exec(@sql)   
end /* while */

close EmployeeCursor
deallocate EmployeeCursor
Joe Stefanelli
Thanks, this looks interesting but how to use cursor...sorry for asking :-(
Tony
@Tony: I've written everything you need for the cursor in the answer.
Joe Stefanelli
Ok Thanks Joe, let me try it out.... will let you know how it turns out......
Tony
Hi Joe, I get back this error and it didn't import any pictures. Does the Employee_id total has to match exacly the total number of pictures or........ Msg 4860, Level 16, State 1, Line 1 Cannot bulk load. The file "c:\images\25662.jpg" does not exist.
Tony
I have an id = 025662 not 25662. I am just testing 8 records out of 400 Employees in the table
Tony
OK, my code was just an example. You'll have to modify it to fit your situation. Instead of declaring `@Employee_id` as an INT, declare it to match your datatype.
Joe Stefanelli
A ok, I have the id field as nvarchar and I declared is as int above...ok let me sort it out...
Tony
Msg 4860, Level 16, State 1, Line 1Cannot bulk load. The file "c:\images\025662.jpg" does not exist.
Tony
I changed it it gives me above message...
Tony
So it looks it has to match the exact records in image folder and in table....
Tony
@Tony: Again, I think you may be taking my example too literally. Are your images stored in `c:\images` or another location? You need to substitute the correct file path. Also, if necessary, adjust the `.jpg` extension to match your real files as well.
Joe Stefanelli
Thanks Joe, I managed to do it however I needed to have matching records on both sides...then it worked....
Tony
yes my images are stored on c:\images and also the .jpg is correct....
Tony
@Tony: Yes, since in your question you said you had pictures for *all* employees, I assumed there would be a match on both sides.
Joe Stefanelli
ok I got this figured out, now I have second issue, On the backend I am using MS Access and I can not see the image on the form, it is long binary data....hm... is this for another post?
Tony
Thanks a lot everybody for your help, specially Joe....
Tony