Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Print() to print more than 8000 characters

How can I print a string (e.g. a dynamic query) that contains more than 8000 chars?

Declare @sql varchar(max)
set @Qry='....(more than 8000 char)'
Print (@Qry)

The above only prints the first 8000 characters of @Qry and cuts off the rest.

like image 503
anoopmishra217 Avatar asked Oct 30 '25 00:10

anoopmishra217


1 Answers

The problem is that VARCHAR(MAX) holds up to 2gb of data, but print() only prints 8000, characters to the terminal. So you have to break up your string into 8000 char chunks and print them separately. e.g.

declare @test varchar(max);
declare @loop int = 1;
declare @length int =0;
declare @printed int =0;

/*build an exceptionally long string.*/
set @test= 'select ''1000000000000000000000000000000000000000000''';

while @loop < 1001 
begin
     set @test = @test + ',''12345678901234567890123456789012345678901234567890123456789012345678901234567890''';
     set @loop = @loop + 1;

end;
/*!build an exceptionally long string.*/

set @length = len(@test);
while @printed < @length
BEGIN
    print(substring(@test,@printed,8000));
    set @printed = @printed + 8000; 
END
like image 164
JeffUK Avatar answered Nov 02 '25 21:11

JeffUK



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!