New SQL Server sp_helptext to avoid line splits of code


By:   |   Updated: 2016-06-22   |   Comments (4)   |   Related: More > T-SQL

Problem

Using sp_helptext to script out or see the definition of a SQL Server stored procedure, trigger or function is very helpful, but when the code has a line with a length more than 256 characters, sp_helptext splits the line at 256 characters and puts the rest of the text on a new line.

Solution

To solve this problem, I created a new sp_helptext stored procedure, but before we look at I will show you the issue we are trying to fix.

Here I create a procedure with a line of length more than 256 as shown below.

CREATE Procedure LineTrunCheck
as 
begin 
print 'HiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiHiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiHiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiHiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiHiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiHiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiHiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii'
end

After it is created, we can use sp_helptext to see the definition of the procedure.

exec sp_helptext LineTrunCheck

From the below image we can see the line got divided into three lines.

sp_helptext splitting lines of code more than 256 characters

For small procedures this is not a big problem, but a procedure with thousands of lines of code this becomes a problem because sometimes key words like INNER JOIN may split into INN and the new line begins with ERR JOIN as shown below.  Or comment lines may get broken apart as shown below.  It becomes difficult to check each and every line for procedures with thousands of lines.

sp_helptext example splitting code causing diffculty to read

Improved sp_helptext SQL Server Stored Procedure

I modified sp_helptext with @SyscomText nvarchar(max), @Line nvarchar(max) and created a procedure sp_helptext2 in the master database

USE master
GO

CREATE procedure [dbo].[sp_helptext2]
@objname nvarchar(776)
,@columnname sysname = NULL
as

set nocount on

declare @dbname sysname
,@objid int
,@BlankSpaceAdded   int
,@BasePos       int
,@CurrentPos    int
,@TextLength    int
,@LineId        int
,@AddOnLen      int
,@LFCR          int --lengths of line feed carriage return
,@DefinedLength bigint



/* NOTE: Length of @SyscomText is 4000 to replace the length of
** text column in syscomments.
** lengths on @Line, #CommentText Text column and
** value for @DefinedLength are all 255. These need to all have
** the same values. 255 was selected in order for the max length
** display using down level clients
*/
,@SyscomText nvarchar(max)
,@Line          nvarchar(max)

select @DefinedLength = 4000
select @BlankSpaceAdded = 0 /*Keeps track of blank spaces at end of lines. Note Len function ignores
                             trailing blank spaces*/
CREATE TABLE #CommentText
(LineId int
 ,Text  nvarchar(4000) collate database_default)

/*
**  Make sure the @objname is local to the current database.
*/
select @dbname = parsename(@objname,3)
if @dbname is null
 select @dbname = db_name()
else if @dbname <> db_name()
        begin
                raiserror(15250,-1,-1)
                return (1)
        end

/*
**  See if @objname exists.
*/
select @objid = object_id(@objname)
if (@objid is null)
        begin
  raiserror(15009,-1,-1,@objname,@dbname)
  return (1)
        end

-- If second parameter was given.
if ( @columnname is not null)
    begin
        -- Check if it is a table
        if (select count(*) from sys.objects where object_id = @objid and type in ('S ','U ','TF'))=0
            begin
                raiserror(15218,-1,-1,@objname)
                return(1)
            end
        -- check if it is a correct column name
        if ((select 'count'=count(*) from sys.columns where name = @columnname and object_id = @objid) =0)
            begin
                raiserror(15645,-1,-1,@columnname)
                return(1)
            end
    if (ColumnProperty(@objid, @columnname, 'IsComputed') = 0)
  begin
   raiserror(15646,-1,-1,@columnname)
   return(1)
  end

        declare ms_crs_syscom  CURSOR LOCAL
        FOR select text from syscomments where id = @objid and encrypted = 0 and number =
                        (select column_id from sys.columns where name = @columnname and object_id = @objid)
                        order by number,colid
        FOR READ ONLY

    end
else if @objid < 0 -- Handle system-objects
 begin
  -- Check count of rows with text data
  if (select count(*) from master.sys.syscomments where id = @objid and text is not null) = 0
   begin
    raiserror(15197,-1,-1,@objname)
    return (1)
   end
   
  declare ms_crs_syscom CURSOR LOCAL FOR select text from master.sys.syscomments where id = @objid
   ORDER BY number, colid FOR READ ONLY
 end
else
    begin
        /*
        **  Find out how many lines of text are coming back,
        **  and return if there are none.
        */
        if (select count(*) from syscomments c, sysobjects o where o.xtype not in ('S', 'U')
            and o.id = c.id and o.id = @objid) = 0
                begin
                        raiserror(15197,-1,-1,@objname)
                        return (1)
                end

        if (select count(*) from syscomments where id = @objid and encrypted = 0) = 0
                begin
                        raiserror(15471,-1,-1,@objname)
                        return (0)
                end

  declare ms_crs_syscom  CURSOR LOCAL
  FOR select text from syscomments where id = @objid and encrypted = 0
    ORDER BY number, colid
  FOR READ ONLY

    end

/*
**  else get the text.
*/
select @LFCR = 2
select @LineId = 1

OPEN ms_crs_syscom

FETCH NEXT from ms_crs_syscom into @SyscomText

WHILE @@fetch_status >= 0
begin

    select  @BasePos   = 1
    select  @CurrentPos = 1
    select  @TextLength = LEN(@SyscomText)

    WHILE @CurrentPos  != 0
    begin
        --Looking for end of line followed by carriage return
        select @CurrentPos =   CHARINDEX(char(13)+char(10), @SyscomText, @BasePos)

        --If carriage return found
        IF @CurrentPos != 0
        begin
            /*If new value for @Lines length will be > then the
            **set length then insert current contents of @line
            **and proceed.
            */
            while (isnull(LEN(@Line),0) + @BlankSpaceAdded + @[email protected] + @LFCR) > @DefinedLength
            begin
                select @AddOnLen = @DefinedLength-(isnull(LEN(@Line),0) + @BlankSpaceAdded)
                INSERT #CommentText VALUES
                ( @LineId,
                  isnull(@Line, N'') + isnull(SUBSTRING(@SyscomText, @BasePos, @AddOnLen), N''))
                select @Line = NULL, @LineId = @LineId + 1,
                       @BasePos = @BasePos + @AddOnLen, @BlankSpaceAdded = 0
            end
            select @Line    = isnull(@Line, N'') + isnull(SUBSTRING(@SyscomText, @BasePos, @[email protected] + @LFCR), N'')
            select @BasePos = @CurrentPos+2
            INSERT #CommentText VALUES( @LineId, @Line )
            select @LineId = @LineId + 1
            select @Line = NULL
        end
        else
        --else carriage return not found
        begin
            IF @BasePos <= @TextLength
            begin
                /*If new value for @Lines length will be > then the
                **defined length
                */
                while (isnull(LEN(@Line),0) + @BlankSpaceAdded + @[email protected]+1 ) > @DefinedLength
                begin
                    select @AddOnLen = @DefinedLength - (isnull(LEN(@Line),0) + @BlankSpaceAdded)
                    INSERT #CommentText VALUES
                    ( @LineId,
                      isnull(@Line, N'') + isnull(SUBSTRING(@SyscomText, @BasePos, @AddOnLen), N''))
                    select @Line = NULL, @LineId = @LineId + 1,
                        @BasePos = @BasePos + @AddOnLen, @BlankSpaceAdded = 0
                end
                select @Line = isnull(@Line, N'') + isnull(SUBSTRING(@SyscomText, @BasePos, @[email protected]+1 ), N'')
                if LEN(@Line) < @DefinedLength and charindex(' ', @SyscomText, @TextLength+1 ) > 0
                begin
                    select @Line = @Line + ' ', @BlankSpaceAdded = 1
                end
            end
        end
    end

 FETCH NEXT from ms_crs_syscom into @SyscomText
end

IF @Line is NOT NULL
    INSERT #CommentText VALUES( @LineId, @Line )

select Text from #CommentText order by LineId

CLOSE  ms_crs_syscom
DEALLOCATE  ms_crs_syscom

DROP TABLE  #CommentText

return (0) -- sp_helptext

Now we will use sp_helptext2 to see the definition of the stored procedure.

exec sp_helptext2 LineTrunCheck

As we can see below the line with a length of more than 256 characters displays correctly.

sp_helptext2 maintains code length
Next Steps
  • Need to create the above stored procedure in the master database so you can use it in all databases on the SQL Server instance.
  • Download sp_helptext2


Last Updated: 2016-06-22


get scripts

next tip button



About the author
MSSQLTips author Ranga Babu Ranga Babu is a SQL Server DBA with experience on performance tuning and high availability.

View all my tips





Comments For This Article




Thursday, December 12, 2019 - 9:34:16 AM - Ranga Back To Top (83406)

Adjust you SSMS properties to accomdate more than 256 char.


Thursday, December 12, 2019 - 8:32:23 AM - Nagaraj Back To Top (83405)

In this sp the balance text is not in the output.

While using the sp_helptext2 missing text for lines longer than 256 characters.


Wednesday, June 27, 2018 - 2:45:54 AM - VISHNUVARDHAN REDDY Back To Top (76428)

Ok ji your explanation is super...

Eventhough have some issue with my query text in some sp's

as per your explaination data more than 255 charecters it should come to next line in some sps charects less than 20 also getting the issue with the sp_help text how to get rid of that can you please explain..

 


Friday, March 31, 2017 - 11:51:38 AM - Dhruv Back To Top (54037)

Thanks Ranga Babu. Your code really helped me.



download


Recommended Reading

Rolling up multiple rows into a single row and column for SQL Server data

SQL Server Loop through Table Rows without Cursor

Using MERGE in SQL Server to insert, update and delete at the same time

Cursor in SQL Server

How to use @@ROWCOUNT in SQL Server





get free sql tips
agree to terms


Learn more about SQL Server tools