Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I upload a file to a varbinary(max) column in SQL Server 2008, using TSQL?

Tags:

This must be possible because I believe I've done it before. Here's my query:

insert into exampleFiles Values(NEWID(), cast('c:\filename.zip' as varbinary(max)) 

Obviously that just inserts the text in between the quotes and not the file from that location. There must be a simple tsql bit of language that I'm forgetting. Thanks

like image 569
chum of chance Avatar asked Nov 25 '09 21:11

chum of chance


People also ask

What is the max size of VARBINARY in SQL Server?

varbinary [ ( n | max) ] Variable-length binary data. n can be a value from 1 through 8,000. max indicates that the maximum storage size is 2^31-1 bytes. The storage size is the actual length of the data entered + 2 bytes.

What is VARBINARY data type in SQL Server?

The VARBINARY type is similar to the VARCHAR type, but stores binary byte strings rather than non-binary character strings. M represents the maximum column length in bytes. It contains no character set, and comparison and sorting are based on the numeric value of the bytes.

When would you use VARBINARY data type?

The VARBINARY data type holds variable-length binary data. Use this type when the data is expected to vary in size. The maximum size for VARBINARY is 8,000 bytes.


1 Answers

Does this help?

USE AdventureWorks GO CREATE TABLE myTable(FileName nvarchar(60),   FileType nvarchar(60), Document varbinary(max)) GO  INSERT INTO myTable(FileName, FileType, field_varbinary)    SELECT 'Text1.txt' AS FileName,       '.txt' AS FileType,       * FROM OPENROWSET(BULK N'C:\Text1.txt', SINGLE_BLOB) AS Document GO 

Taken from here: http://social.msdn.microsoft.com/Forums/en-US/sqltools/thread/513cbf8c-21a8-4bcc-a565-6eb06437a398

like image 53
dcp Avatar answered Oct 27 '22 20:10

dcp