Thursday 14 January 2021

How to run a more than 8000 characters SQL statement from a variable?

 Description:

In this example, we explain that How to run a more than 8000 characters SQL statement from a variable? Or varchar (MAX) text cuts off while going more than 8000 characters inside a procedure. Or how to print the SQL Query when it exceeds the 8000 character. Because the problem was that by default Nvarchar (max) only print 8000 characters so when your query exceeds the 8000 characters at that time what should you do if you want to debug or print the string sql query.


So to achieve that below is the example that will help you to print the whole query in stored procedure and you can easily investigate that.

below query you will used at the end of the character limit.


Query:

declare @i int = 1

 

                        while Exists(Select(Substring(@queryString,@i,4000))) and (@i < LEN(@queryString))

 

                        begin

 

                        print Substring(@queryString,@i,4000)

 

                        set @i = @i+4000

 

                        end

 

 print ('1 -'+ @queryString)

This entry was posted in :

1 comments:

  1. Wow, that was informative. Thank you for sharing this. We are a Master MSP (simply put, IT Service and Support Providers) based in India and we have partnered with and served 75+ MSPs globally in the past 6 years. You can know more about us by visiting our
    infrassist
    outlook rules not working
    80090016
    powershell script
    script hub
    join Microsoft Teams Meeting

    ReplyDelete