Sylvain B.
Sylvain B.

Reputation: 739

Insert file content in `varbinary` column

I need to write a simple SQL script to insert a new line to a table with a varbinary column that is supposed to hold the content of files. I have tried :

DECLARE @FileContent varbinary(max)
SET @FileContent = SELECT * FROM OPENROWSET (BULK 'pathToFile', SINGLE_BLOB);

INSERT INTO [MyTable] ([Name], [Content])
VALUES ('Dummy', @FileContent)

But it does not compile...

Upvotes: 2

Views: 2700

Answers (1)

Mudassir Hasan
Mudassir Hasan

Reputation: 28751

Enclose in round brackets SELECT * FROM OPENROWSET

DECLARE @FileContent varbinary(max)
SET @FileContent = (SELECT * FROM OPENROWSET (BULK 'pathToFile', SINGLE_BLOB) tmp);

INSERT INTO [MyTable] ([Name], [Content])
VALUES ('Dummy', @FileContent)

Upvotes: 4

Related Questions