How to Insert Binary Data into a SQL Server Database

In the following example we will create a table with three columns: ID, Title and MyImage:

CREATE TABLE ImageTable(ID int, Title nvarchar(120), MyImage varbinary(max));
GO

"MyImage" column is a varbinary(max) column, and we will insert data from a file named test_image.jpg from E:\temp directory into it:

INSERT INTO ImageTable(ID, Title, MyImage)
SELECT '1' AS ID,
'My Image 1' AS Title,
* FROM OPENROWSET(BULK N'E:\temp\test_image.jpg', SINGLE_BLOB) AS MyImage;
GO

The data is inserted into the table:

sql_server_insert_binary_data

To update the row, and change the binary data in the column:

UPDATE ImageTable
SET MyImage =
(SELECT * FROM OPENROWSET(BULK N'E:\temp\another_image.jpg', SINGLE_BLOB) AS MyImage)
WHERE ID = 1;
GO

If the post helped you, please share it:
Pin It

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">