Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server - How to insert into Varbinary(Max) column?

I have a table that looks as follows below. I don't really want to create a C# application to insert rows into this table, if I can avoid it, because of the VarBinary column. My intent is to store a Crystal report .RPT file in this column. Is there a T-SQL statement I can execute to insert/update rows into this table, and include an .RPT file?

CREATE TABLE [Report].[MesReport](
    [MesReportID] [int] IDENTITY(1,1) NOT NULL,
    [ParentID] [int] NOT NULL,
    [ReportTitle] [nvarchar](80) NOT NULL,
    [ReportName] [nvarchar](80) NOT NULL,
    [DatabaseServer] [nvarchar](80) NOT NULL,
    [DatabaseName] [nvarchar](50) NOT NULL,
    [Login] [nvarchar](80) NOT NULL,
    [ReportFile] [varbinary](max) NULL,
like image 479
Randy Minder Avatar asked Jan 21 '23 16:01

Randy Minder


1 Answers

You can get it into a variable like

DECLARE @VB varbinary(max)
SELECT @VB =BulkColumn FROM OPENROWSET(BULK
     N'C:\YourReport.rpt', SINGLE_BLOB) AS Document

that you can then use in an insert statement

like image 105
Martin Smith Avatar answered Feb 01 '23 14:02

Martin Smith