How to use a variable in Openrowset command How to use a variable in Openrowset command sql sql

How to use a variable in Openrowset command


Change your script like below.

DECLARE @file_stream VARBINARY(MAX)DECLARE @command nvarchar(1000)DECLARE @filePath NVARCHAR(128)set @filePath = 'C:\Temp\print4.pdf'set @command = N'SELECT @file_stream1 = CAST(bulkcolumn AS varbinary(MAX))                from OPENROWSET(BULK ''' + @filePath + ''',                SINGLE_BLOB) ROW_SET'EXEC sp_executesql @command, N'@file_stream1 VARBINARY(MAX) OUTPUT',@file_stream1 =@file_stream OUTPUTselect @file_stream

Sample Output :enter image description here