August 3, 2023 at 10:13 am
I have some code which will load in and process a JSON file using Select..from Openrowset as follows:
Declare @JSON varchar(max)
SELECT @JSON = BulkColumn
FROM OPENROWSET (BULK 'C:\Users\User1\DR\staging\returns\RET02022203.JSON', SINGLE_CLOB) as j
select @JSON
i tried to turn this into Dynamic SQL so that I can pass in the path to the JSON file as a variable but the code is not working and the JSON is not being selected
My attempt at writing this in dynamic SQL is as follows:
DECLARE @FilePath NVARCHAR(500) = N'C:\Users\User1\DR\staging\returns\RET02022203.JSON'
DECLARE @JSON VARCHAR(MAX)
DECLARE @sql NVARCHAR(MAX)
DECLARE @params NVARCHAR(255) = '@JSON VARCHAR(MAX) OUTPUT'
SET @SQL = 'SELECT' + @JSON + ' = BulkColumn FROM OPENROWSET (BULK ' + '''' + @FilePath + '''' + ', SINGLE_CLOB) as j'
PRINT @SQL
EXEC sp_executesql @sql, @params, @JSON = @JSON OUTPUT;
SELECT @JSON
What am i doing wrong here ?
August 3, 2023 at 10:53 am
DECLARE @FilePath NVARCHAR(500) = N'C:\Users\User1\DR\staging\returns\RET02022203.JSON'
DECLARE @JSON NVARCHAR(MAX)
DECLARE @sql NVARCHAR(MAX)
SET @SQL = 'SELECT @JSON = BulkColumn FROM OPENROWSET (BULK ' + '''' + @FilePath + '''' + ', SINGLE_CLOB) as j'
PRINT @SQL
EXEC sp_executesql @sql, '@JSON nvarchar(MAX) OUTPUT', @JSON = @JSON OUTPUT;
PRINT @JSON;
Viewing 2 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy