Skip to content

Instantly share code, notes, and snippets.

@Vladimir-Novick
Last active August 17, 2023 12:59
Show Gist options
  • Save Vladimir-Novick/7babd786e5537241a320b12eaf340ac3 to your computer and use it in GitHub Desktop.
Save Vladimir-Novick/7babd786e5537241a320b12eaf340ac3 to your computer and use it in GitHub Desktop.
Upload file to SQL server into varbynary(max) using SQL Server function OPENROWSET and BULK
CREATE TABLE myTable(FileName nvarchar(60),
FileType nvarchar(60), Document varbinary(max))
INSERT INTO myTable(FileName, FileType, Document)
SELECT 'ConnectionManager.pdb' AS FileName,
'.pdb' AS FileType,
* FROM OPENROWSET(BULK N'D:\TEST\BIN\ConnectionManager.pdb', SINGLE_BLOB) AS Document
@Vladimir-Novick
Copy link
Author

You can use DATALENGTH() function to determine the length of the column content:

select DATALENGTH(Document),Document from myTable where FileName='ConnectionManager.pdb'

@Vladimir-Novick
Copy link
Author

The VARBINARY(MAX) field allocates variable length data up to just under 2GB in size.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment