Sql server 有没有办法在SQL Server存储过程中查询注释?

Sql server 有没有办法在SQL Server存储过程中查询注释?,sql-server,tsql,stored-procedures,Sql Server,Tsql,Stored Procedures,我希望显示存储过程的文档步骤。最好的开始方法是从存储过程中提取所有注释。有没有办法只查询并返回我的存储过程中的注释?对于以“-”开头的注释,可以这样使用: DECLARE @storedproc varchar(8000) = '-- Some Comments SET NOCOUNT ON; -- Some Comments SELECT FirstName -- we only need the first name FROM dbo.Users WHERE Id = @Id;'; S

我希望显示存储过程的文档步骤。最好的开始方法是从存储过程中提取所有注释。有没有办法只查询并返回我的存储过程中的注释?

对于以“-”开头的注释,可以这样使用:

DECLARE @storedproc varchar(8000) =
'-- Some Comments
SET NOCOUNT ON;

-- Some Comments

SELECT FirstName -- we only need the first name
FROM dbo.Users WHERE Id = @Id;';

SELECT *
FROM 
(
  SELECT
    lineNumber = row_number() over (ORDER BY d.p),
    wholeLine  = SUBSTRING
               (
                 @storedproc, 
                 d.p+l.d, -- delimiter position + delimiter length
                  isnull(nullif(charindex(char(10), @storedproc, d.p+l.d),0) - (d.p+l.d), l.s+l.d)
               )
  FROM (values (len(@storedproc), 1)) as l(s,d) -- length of the string and delimiter as "inline variables"
  CROSS APPLY
  (
    SELECT -(l.d) UNION ALL -- 0 would be fine too; I'm using -(l.d) to keep the formula uniform
    SELECT ng.position
    FROM dbo.NGrams8K(@storedproc, l.d) as ng
    WHERE token = char(10)
  ) as d(p) -- delimiter.position
) split
CROSS APPLY (VALUES (SUBSTRING(wholeLine, charindex('--', wholeLine), 8000))) txt(comment)
WHERE txt.comment LIKE '%--%';
返回:

lineNumber  wholeLine                                         comment
----------  ----------------------------------------------    -----------------------
1           -- Some Comments                                  -- Some Comments
4           -- Some Comments                                  -- Some Comments
6           SELECT FirstName  -- we only need the first name  -- we only need the first name
对于长度超过8000个字符的进程,请使用并将“我的代码”中8000的每个实例更改为输入字符串的长度