14

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?

1

2 Answers 2

33
UPDATE YourTable
SET BlobColumn = 
    (SELECT  BulkColumn FROM OPENROWSET(BULK  N'C:\YourFile.png', SINGLE_BLOB) AS x)
WHERE ...
2
  • 1
    Exactly what I needed. Simple and effective. Thanks!
    – nrod
    Jan 13, 2015 at 8:42
  • 3
    Just remember that if you're connecting to a remote database server, the path is relative to the server that the DB is on, not your local machine.
    – Ads
    Aug 23, 2016 at 3:29
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.