I am trying to create an SP that will return JSON data from a specified table. I want to store this in a variable to format and concat additional information.
This works well when the dataset is small. However, this SP is only returning the first 8000 characters when large. Is there a way to use the same SP to return longer strings?
Using Azure Studio and just executing the select returns string longer than 8000, but there is no way to store in a variable.
CREATE OR ALTER PROCEDURE [dbo].[spGetJsonFromTable]
(
@tableName VARCHAR(128),
@jsonString VARCHAR(MAX) OUTPUT
)
AS
BEGIN
DECLARE @SQL NVARCHAR(4000);
SET @SQL = N'SET @jsonString = (SELECT * FROM ' + @tableName + N' FOR JSON AUTO, INCLUDE_NULL_VALUES);';
EXEC [sys].[sp_executesql] @SQL,
N'@jsonString VARCHAR(MAX) OUT',
@jsonString OUT;
END;
GO
This is the call to SP
DECLARE @jsonString VARCHAR(MAX);
EXEC [dbo].[spGetJsonFromTable] @tableName = 'mytable', -- varchar(128)
@jsonString = @jsonString OUTPUT; -- varchar(max)