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
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.