16

Is it possible to insert a file in a varbinary(max) column in Transact-SQL? If yes, I would be very please to have a code snippet to at least give me an idea how to do that.

Thanks

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Yannic
  • 229
  • 1
  • 3
  • 4

1 Answers1

29

It's so easy - once you know it! :-) Found this on Greg Duncan's blog a while ago:

INSERT INTO YourTable(YourVarbinaryColumn)
    SELECT * FROM 
    OPENROWSET(BULK N'(name of your file to import)', SINGLE_BLOB) AS import

And here's the MSDN library documentation on it.

Marc

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 13
    remember the path to the file is relative to the SQL server. So if it is C:\test.txt that is on the c: drive of the computer the sql server instance is running on. – Tony Sep 19 '11 at 19:35
  • 1
    Note that the **AS import** at the end is very important! – IanGSY Dec 31 '15 at 11:55