Sunday, 19 August 2018

For Nvarchar(Max) I am only getting 4000 characters in SQL Server?

nvarchar max after 4000 characters not working in stored procedure

Description:
In this example we explain that how to use SQL string variable larger than 4000 character in SQL Server Stored procedure because we all know that SQL will not execute Query parameter contains more than 4000 character so to overcome this problem we have to use the while loop and assign the string in a variable.

There are many questions are raised from user like nvarchar max after 4000 character not working. Or trouble printing out long varchar max string.so below is the query that will help you when you SQL Query size more than 4000 character.
Query:

print len(@Query);
            declare @i int = 1
                        while Exists(Select(Substring(@Query,@i,4000))) and (@i < LEN(@Query))
                        begin
                       
                        print Substring(@Query,@i,4000)
                        set @i = @i+4000
                        end
            print @i;



This entry was posted in :

0 comments:

Post a Comment