SQL server 2012 SP_HELPTEXT extra lines issue

前端 未结 8 1778
生来不讨喜
生来不讨喜 2020-12-31 01:33

I am using SQL server 2012, & always use SP_HELPTEXT to get my previously created Stored Procedures, In previous versions of SQL server there were no issues

相关标签:
8条回答
  • 2020-12-31 01:55

    There are two type of solution which you can use for remove extra space from sp_helptext output

    1. Goto Tools>Option>Query Results>Result to text (From drop down list)
    2. Copy sp_helptext output Paste in Notepad++. open find and replace window by pressing cntl+H enter the following text to replace with new text as in given picture. enter image description here
    0 讨论(0)
  • 2020-12-31 01:59

    A better workaround (compared to use Results to text) in my opinion is to create an sp_helptext2 storedproc as explained here:

    http://sql-javier-villegas.blogspot.com/2012/08/a-workaround-for-sphelptext-bug-in-ssms.html

    Note: that solution has a bug leaving out the last line if there is no new line at the end. Corrected T-SQL:

    CREATE PROCEDURE [dbo].[sp_helptext2] (@ProcName NVARCHAR(256))
    AS
    BEGIN
      DECLARE @PROC_TABLE TABLE (X1  NVARCHAR(MAX))
    
      DECLARE @Proc NVARCHAR(MAX)
      DECLARE @Procedure NVARCHAR(MAX)
      DECLARE @ProcLines TABLE (PLID INT IDENTITY(1,1), Line NVARCHAR(MAX))
    
      SELECT @Procedure = 'SELECT DEFINITION FROM '+db_name()+'.SYS.SQL_MODULES WHERE OBJECT_ID = OBJECT_ID('''+@ProcName+''')'
    
      insert into @PROC_TABLE (X1)
            exec  (@Procedure)
    
      SELECT @Proc=X1 from @PROC_TABLE
    
      WHILE CHARINDEX(CHAR(13)+CHAR(10),@Proc) > 0
      BEGIN
            INSERT @ProcLines
            SELECT LEFT(@Proc,CHARINDEX(CHAR(13)+CHAR(10),@Proc)-1)
            SELECT @Proc = SUBSTRING(@Proc,CHARINDEX(CHAR(13)+CHAR(10),@Proc)+2,LEN(@Proc))
      END
      --* inserts last line
      insert @ProcLines 
      select @Proc ;
    
      SELECT Line FROM @ProcLines ORDER BY PLID
    END
    
    0 讨论(0)
  • 2020-12-31 02:03

    I've been using Rufo's solution, but I just realized it breaks when the database has a dash. Example: [ST-SALES].

    For what it's worth, this is an updated answer based on Rufo's solution.

    ALTER PROCEDURE [dbo].[sp_helptext2] (@ProcName NVARCHAR(256))
    AS
    BEGIN
      DECLARE @PROC_TABLE TABLE (X1  NVARCHAR(MAX))
    
      DECLARE @Proc NVARCHAR(MAX)
      DECLARE @Procedure NVARCHAR(MAX)
      DECLARE @ProcLines TABLE (PLID INT IDENTITY(1,1), Line NVARCHAR(MAX))
    
      SELECT @Procedure = 'SELECT DEFINITION FROM ['+db_name()+'].SYS.SQL_MODULES WHERE OBJECT_ID = OBJECT_ID('''+@ProcName+''')'
    
      insert into @PROC_TABLE (X1)
            exec  (@Procedure)
    
      SELECT @Proc=X1 from @PROC_TABLE
    
      WHILE CHARINDEX(CHAR(13)+CHAR(10),@Proc) > 0
      BEGIN
            INSERT @ProcLines
            SELECT LEFT(@Proc,CHARINDEX(CHAR(13)+CHAR(10),@Proc)-1)
            SELECT @Proc = SUBSTRING(@Proc,CHARINDEX(CHAR(13)+CHAR(10),@Proc)+2,LEN(@Proc))
      END
      --* inserts last line
      insert @ProcLines 
      select @Proc ;
    
      SELECT Line FROM @ProcLines ORDER BY PLID
    END
    
    0 讨论(0)
  • 2020-12-31 02:08

    I can replicate this behaviour if I run sp_helptext with Results to grid set, then copy and paste the results from grid into a new query or any other text editor.

    This seems to be a change in the behaviour of sp_helptext from previous editions, since this effect isn't displayed with standard grid result sets.

    The simplest work-around will be to run sp_helptext with Results to text set (Query -> Results to > Results to text, shortcut CTRL + T.

    You may need to increase the maximum number of characters per line in Results to text to get the output you expect - Tools > Options > Query Results > Results to text - set "maximum number of characters displayed in each column" to the maximum value of 8192.

    0 讨论(0)
  • 2020-12-31 02:13

    Use the below query to get exact SP query without extra spaces:

    (SELECT  OBJECT_DEFINITION (OBJECT_ID('SPname' )))
    
    0 讨论(0)
  • 2020-12-31 02:16

    I too faced this issue for a view.

    Below are steps i used to resolve the issue (note only a temporary fix)

    1)select the query

    2)then right click and open in query designer

    3)click ok

    you would notice all the extra spaces are removed.

    Note: this works only for simple queries not for procedures,etc

    0 讨论(0)
提交回复
热议问题