0

I want to store a FOR JSON AUTO output in a variable.

But I want to use a variable select statement.

Something like this:

SET @SQLString = 'SELECT ID, Name FROM ' + @TableName + ' ';

SET @Body = (
   @SQLString + 'FOR JSON AUTO'
); 
2

1 Answer 1

1

Like this

declare @sql nvarchar(max) = 'select * from sys.objects'

declare @forJsonSql nvarchar(max) = concat('select @json = (',@sql, ' for json auto)')

--print @forJsonSql

declare @json nvarchar(max)
exec sp_executesql @forJsonSql, N'@json nvarchar(max) output', @json=@json output

select @json

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.