如何打印包含超过8000个字符的字符串(例如动态查询)?
Declare @sql varchar(max)
set @Qry='....(more than 8000 char)'
Print (@Qry)
上面的内容仅打印@Qry的前8000个字符,并剪去其余的字符。
答案 0 :(得分:0)
问题在于VARCHAR(MAX)最多可容纳2gb的数据,但是print()仅向终端打印8000个字符。因此,您必须将字符串分成8000个字符块,然后分别打印。例如
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