Tuesday, October 18, 2011

THE PRINT COMMAND CAN ONLY PRINT 8,000 CHARACTERS.PRINT OUT A STRING LONGER THAN THAT.

SET NOCOUNT ON
DECLARE @SQLInput VARCHAR(MAX)
SET @SQLInput = (select replicate('a', 4000))
SET @SQLInput = @SQLInput + (select replicate('b', 4000))
SET @SQLInput = @SQLInput + (select replicate('C', 4000))
SET @SQLInput = @SQLInput + (select replicate('D', 4000))
SET @SQLInput = @SQLInput + (select replicate('E', 4000))
SET @SQLInput = @SQLInput + (select replicate('F', 4000))
SET @SQLInput = @SQLInput + (select replicate('G', 4000))
SET @SQLInput = @SQLInput + (select replicate('H', 4000))
SELECT LEN(@SQLInput)
---legnth of the string is 32000, but when we print with print ststement its print only 8000 characters
print @SQLInput

---Here is the solution for printing A STRING LONGER THAN THAT.
DECLARE @VariableLength NUMERIC(10,2), @Chunk NVARCHAR(4000),
@SubstringStart INT, @SubstringEnd INT
SET @VariableLength = LEN(@SQLInput)
SET @SubstringStart = 0
SET @SubstringEnd = 4000

if @VariableLength <= 4000
begin
print @SQLInput
end else begin
WHILE (@SubstringStart + @SubstringEnd) < @VariableLength
BEGIN
SELECT @SubstringStart = @SubstringStart + CASE @SubstringStart WHEN 0 THEN 1 ELSE @SubstringEnd END
SET @Chunk = SUBSTRING(@SQLInput, @SubstringStart, 4000)
SET @SubstringEnd = LEN(@Chunk) --OTHERWISE, END POSITION IS OK.
print substring(@SQLInput,@SubstringStart, @SubstringEnd)
--SET @PrintSQL = @PrintSQL + 'PRINT SUBSTRING(@SQLInput, ' + CAST(@SubstringStart AS NVARCHAR(10)) + ', ' + CAST(@SubstringEnd AS NVARCHAR(10)) + ')'
END
end

No comments:

Post a Comment