Intro
Unfortunately, the
print command just won't spit out anything past 8000 characters (4000 for unicode).
Code
-- all of the strings follow this pattern, with a different number of >
-- >>>>>\r\n.
-- ^^^^^ 3997 of these, so total length of 4000
declare @s nvarchar(max)
-------------------------------------------------------------------------------
-- try #1: just print out a 4000-character unicode string
set @s = replicate('>', 3997) + char(13) + char(10) + '.'
print 'try 1'
print @s
-------------------------------------------------------------------------------
-- try #2: make that 4000-character unicode string one character longer
set @s = replicate('>', 3998) + char(13) + char(10) + '.'
print 'try 2'
print @s
-------------------------------------------------------------------------------
-- try #3: skip the first character when printing the 4001 character string
set @s = replicate('>', 3998) + char(13) + char(10) + '.'
print 'try 3'
print substring(@s, 2, 4000)
-------------------------------------------------------------------------------
-- try #4: now print a 12001 character string, 4000 characters at a time
set @s = replicate('>', 4000)
set @s = @s + replicate('>', 4000)
set @s = @s + replicate('>', 4000)
set @s = @s + '.'
declare @n int
set @n = 0
while @n * 4000 < len(@s)
begin
print 'try 4, #' + cast(@n as varchar)
print substring(@s, @n * 4000 + 1, 4000)
set @n = @n + 1
end
Output
Note that the workaround that loops through the
nvarchar(max) variable adds line breaks every 4000th character.
try 1
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>...
.
try 2
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>...
try 3
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>...
.
try 4, #0
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>...
try 4, #1
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>...
try 4, #2
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>...
try 4, #3
.