我们有一个非常自动化的数据库,用于工作中的内部报告。它运行在很多通过sqlcmd执行的存储过程上,随着存储过程列表变得越来越大,并且手动维护每个SP正在做什么的文档在我看来像折磨我开始想到一个查询会得到我所有用户定义的程序列表及其内置代码描述。
示例程序名称 - dbo.RemedySelect 描述块:
-- =============================================
-- Author: author_id
-- Create date: 2015/08/03
-- Description: {Insert data from Remedy Report into SRC_REMEDY_RAW table}
-- Batch name: remedy_import.cmd
-- =============================================
答案 0 :(得分:1)
我可能会跳过使用大括号并使用它:
select
p.object_id as [SP_ID]
,p.[name] as [SP_NAME]
,p.create_date as [CREATE_DATE]
,p.modify_date as [MODIFY_DATE]
,ltrim(rtrim(substring(
c.[definition],
charindex('-- Description:',c.[definition]) + len('-- Description:') + 1,
charindex('--', c.[definition], charindex('-- Description:',c.[definition]) + 2) - charindex('-- Description:',c.[definition]) - len('-- Description:') - 3
))) as [SP_DESCRIPTION]
from sys.objects p
join sys.sql_modules c
on p.object_id = c.object_id
where p.[type] = 'P'
and p.is_ms_shipped = 0
and c.[definition] like '%-- Description:%[' + CHAR(13)+CHAR(10)+']%'
order by p.create_date
将处理这样的单行描述:
-- =============================================
-- Author: author_id
-- Create date: 2015/08/03
-- Description: Insert data from Remedy Report into SRC_REMEDY_RAW table
-- Batch name: remedy_import.cmd
-- =============================================
将-- Description:
替换为评论中的其他标题也可以提取其他文档。
答案 1 :(得分:0)
我在SO(link)上找到了一个有用的查询代码,对其进行了一些修改:
select
p.object_id as [SP_ID]
,p.[name] as [SP_NAME]
,p.create_date as [CREATE_DATE]
,p.modify_date as [MODIFY_DATE]
-- get the description string from between '{' and '}' characters
,replace(substring(c.[definition], charindex('{',c.[definition])+1 ,((charindex('}',c.[definition]))-((charindex('{',c.[definition]))))),'}','')as [SP_DESCRIPTION]
from sys.objects p
join sys.sql_modules c
on p.object_id = c.object_id
where p.[type] = 'P'
and p.is_ms_shipped = 0
and c.[definition] like '%Description%'
order by p.create_date
以下是上述查询的结果:
SP_ID SP_NAME CREATE_DATE MODIFY_DATE SP_DESCRIPTION
1613248802 RemedySelect 2015-08-18 09:17:28.880 2016-02-18 17:32:13.983 Insert data from Remedy Report into SRC_REMEDY_RAW table
当有数十个或超过一百个存储过程时,我发现它非常有用。它可以帮助我保持文档的最新状态。 我很乐意听取您对代码和维护存储过程文档的一般方法的反馈。