@@ -684,6 +684,7 @@ declare
684684 ,@LineNum int
685685 ,@WarningLines varchar (max)
686686 ,@buffer nvarchar(max)
687+ ,@line nvarchar(max)
687688
688689select
689690 @IsDac = Ep .is_admin_endpoint
@@ -872,20 +873,29 @@ begin
872873
873874 IF @Debug = 1 RAISERROR(' -- LineNum:%d, Length: %d' ,0 ,1 ,@LineNum,@LineLength) with nowait;
874875
876+ -- if current line overflows the limit, then we print just the limi, and next loop we check the rest.
875877 if @mode = ' text'
876878 if @LineLength > @MaxLineSize
877879 begin
878880 set @LineLength = @MaxLineSize
879881 set @i = @start+ @LineLength;
880882 end
881-
882- if @LineLength <= 0 -- empty line. Add to buffer because print or raiseror add space if print empty line
883- set @buffer + = nchar(13 )+ nchar(10 )
884- else begin
885- set @buffer + = substring (@ObjectDefinition,@start,@LineLength)
886- print @buffer
887- set @buffer = ' ' ;
883+
884+ if @LineLength <= 0
885+ set @LineLength = 0 ;
886+
887+ set @line = substring (@ObjectDefinition,@start,@LineLength) + nchar(13 )+ nchar(10 )
888+
889+ -- we will bufferize max as possible to avoid lot of writes to client, optimizing performance
890+ -- because print limit of 4000, if current line dont fit in buffer, then we flush them to client.
891+ if len(@buffer) + len(@line ) > 4000
892+ begin
893+ if len(@buffer) > 0 -- security for void print if buffer is empty and line is already grather than 4k
894+ print @buffer;
895+ set @buffer = ' ' ;
888896 end
897+
898+ set @buffer + = @line ; -- here, our buffer is ready to acept line!
889899
890900 if @LineLength > @MaxLineSize -- print limit
891901 begin
0 commit comments