How to insert an extended value into a table in a column of type varbinary?

4

A table has a varbinary (max) column, I need it to have an entry with 400MB of data in this column.

How do I create this data?

Does the Insert statement have to have the data in full or can I use a loop or a function that manages this data?

    
asked by anonymous 12.12.2013 / 13:09

3 answers

4

Martin Smith

INSERT INTO [YourTable] (YourFileColumn)
VALUES      ( CAST(REPLICATE(CAST(0XFF AS VARBINARY(MAX)), 
                         100 * 1024 * 1024) AS VARBINARY(MAX)))
    
13.12.2013 / 23:19
5

You can use a OPENROWSET function, for example:

CREATE TABLE Tabela(NomeArquivo nvarchar(100), Arquivo varbinary(max));
GO

INSERT INTO Tabela(NomeArquivo, Arquivo) 
   SELECT 'Texto.txt' AS NomeArquivo, * FROM OPENROWSET(BULK N'C:\Arquivo.txt', SINGLE_BLOB) AS Arquivo;
GO

Reference

    
12.12.2013 / 15:16
2

Friend, although I'm not exactly answering your question the way you expect it, I think putting a column with up to 400Mb per record in the database does not seem like a good idea.

I would only put one URL in the database and would make this heavy data available somewhere on the internet or intranet accessible through the registered URL. This also allows you to distribute access to these files, migrate them, compress them and reuse them without relying on the database to do things that are not the purpose of it.

    
12.12.2013 / 15:16