Skip to content

Instantly share code, notes, and snippets.

@EitanBlumin
Last active September 2, 2020 09:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save EitanBlumin/b2c6a021071aed9182cf57c2bceade85 to your computer and use it in GitHub Desktop.
Save EitanBlumin/b2c6a021071aed9182cf57c2bceade85 to your computer and use it in GitHub Desktop.
This is a minified version of the PrintMax procedure (originally written by Ben Dill). It's created as a temporary procedure.
IF OBJECT_ID('tempdb..#PrintMax') IS NOT NULL DROP PROC #PrintMax;
GO
/*
Author: Eitan Blumin (t: @EitanBlumin | b: eitanblumin.com)
Description:
This is a minified version of the PrintMax procedure (originally written by Ben Dill).
It's created as a temporary procedure.
*/
CREATE PROCEDURE #PrintMax @str NVARCHAR(MAX)
AS
BEGIN
IF (@str IS NULL) RETURN;
DECLARE @LBindex INT,@len INT;
SET @len = 4000;
WHILE (LEN(@str) > @len) BEGIN
SET @LBindex = CHARINDEX((CHAR(10) + CHAR(13)) COLLATE database_default, REVERSE(LEFT(@str, @len)) COLLATE database_default);
IF @LBindex = 0 AND @str COLLATE database_default LIKE N'%' + CHAR(13) + CHAR(10) + N'%'
SET @LBindex = CHARINDEX((N',') COLLATE database_default, REVERSE(LEFT(@str, @len)) COLLATE database_default);
PRINT LEFT(@str, @len - @LBindex + 1);
SET @str = RIGHT(@str, LEN(@str) - @len + @LBindex - 1);
END;
IF (LEN(@str) > 0) PRINT @str;
END;
GO
@EitanBlumin
Copy link
Author

Added fix for edge case where a single line is too long (break by comma instead of endline)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment