help required on json output

Dinesh Kalva 100 Reputation points
2023-11-19T16:35:54.2766667+00:00

Hi, I'm looking for help. For some reason, am not getting full data in output. Here is my code...

i have 10 k records but I am getting only limited record count in @jsonsql. But in @recordcount variable i can see full count. Please suggest me. I have export data to csv / txt file, and file is only 2 mb. Where it is going wrong?

declare @sql nvarchar(max),@jsonsql nvarchar(max), @recountcount int;
set @sql = ';with cte as (select * from emp) select @jsonsql=(select * from cte for json path)', @recountcount=(select count(*) from cte)
exec sp_executesql @sql, N'@jsonsql nvarchar(max) output,@recountcount int output', @jsonsql output, @recountcount int


SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,629 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,620 questions
{count} votes

1 answer

Sort by: Most helpful
  1. LiHongMSFT-4306 26,621 Reputation points
    2023-11-20T02:50:33.9766667+00:00

    Hi @Dinesh Kalva

    How about this modifiction:

    declare @sql nvarchar(max),@jsonsql nvarchar(max), @recountcount int; 
    set @sql = ' 
    ;with cte as  
    ( 
     select * from [dbo].[employees] 
    )  
    select @jsonsql=(select * from cte for json path)       
          ,@recountcount=(select count(*) from cte)'  
    exec sp_executesql @sql,       
         N'@jsonsql nvarchar(max) output,@recountcount int output', 	 
         @jsonsql output, 	 
         @recountcount output
    
    select @jsonsql,@recountcount
    

    Best regards,

    Cosmog Hong


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.