Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server 2005/2008: Insert a File in an varbinary(max) column in Transact-SQL

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

like image 273
Yannic Avatar asked Jul 31 '09 13:07

Yannic


People also ask

What is the max size of VARBINARY in SQL Server?

varbinary [ ( n | max ) ] n can be a value from 1 through 8,000. max indicates that the maximum storage size is 2^31-1 bytes.

Can you store files in an SQL database?

The FileTable feature enables enterprise customers to store unstructured file data, and directory hierarchies, in a SQL Server database. The feature addresses the requirements for non-transactional access and Windows application compatibility for file-based data.

Can you insert into a view?

You can insert rows into a view only if the view is modifiable and contains no derived columns. The reason for the second restriction is that an inserted row must provide values for all columns, but the database server cannot tell how to distribute an inserted value through an expression.


1 Answers

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

like image 191
marc_s Avatar answered Oct 21 '22 10:10

marc_s