MarioH
MarioH

Reputation: 1157

What is the easiest way to update an image field with the content of a file

I've a table in MS Sql server with an image field and a file. What is the easiest way to create a T-Sql script that updates the field with the content of the file?

Upvotes: 14

Views: 34224

Answers (2)

Drop table employees:

CREATE TABLE Employees
(
    myid int,
    myname varchar(50) not null,
    mypic varbinary(max) not null
)

INSERT INTO Employees (myid, myname, mypic) 
SELECT 10, 'John', BulkColumn 
FROM Openrowset( Bulk 'C:\delete\a.bmp', Single_Blob) as EmployeePicture

UPDATE Employees SET [mypic] =
(
    SELECT MyImage.*
    from Openrowset(Bulk 'C:\Delete\B.bmp', Single_Blob) MyImage)
    where myid = 10

After column i.e. MayImage there should be .* otherwise, it will not work.

Upvotes: 0

Martin Smith
Martin Smith

Reputation: 453358

UPDATE YourTable
SET BlobColumn = 
    (SELECT  BulkColumn FROM OPENROWSET(BULK  N'C:\YourFile.png', SINGLE_BLOB) AS x)
WHERE ...

Upvotes: 34

Related Questions