ALTER数据库中的所有存储过程(添加注释标题模板)

时间:2014-06-24 12:50:54

标签: sql-server tsql stored-procedures sql-server-2008-r2 sql-server-2012

我想将以下模板添加到所有存储过程的开头,作为文档策略的一部分,例如:

---------------------------------------------------
--- PURPOSE:
---
--- UPDATED BY --- UPDATED DATE --- DESCRIPTION----
---
---------------------------------------------------

是否可以以某种方式执行此操作,例如,遍历数据库中的所有存储过程并更改它们?

3 个答案:

答案 0 :(得分:3)

你可以通过编写脚本并使用Find& amp;替换:

  1. 右键单击Management Studio中的数据库名称,然后导航到任务>生成脚本
  2. 选择对象页面上,将您编写脚本的项目过滤为存储过程
  3. 设置脚本选项页面上,选择保存到新查询窗口
  4. 完成上述步骤,然后点击完成
  5. 这会将所有存储过程编写到查询窗口。

    然后使用find和replace来更新脚本程序。当我们计划 ALTER 存储过程时,我们需要将 CREATE 替换为 ALTER ,并在 ALTER之前添加注释块

    1. 查找文字设置为创建程序
    2. 替换为文本添加到评论栏+ 更改程序,并注意换行符\n,这是替换字段所必需的。多线。
    3.   

      ----------------------------------------------- ---- \ n ---目的:\ n --- \ n ---更新---更新日期---   说明---- \ n --- \ n -------------------------------------- ------------- \ nALTER   程序

      查找选项中,勾选使用并选择正则表达式以启用替换换行符。

      点击全部替换,检查脚本然后执行以更改所有内容。

      警告:我已经在SSMS 2012中做到了这一点。从quick search我相信脚本选项在2008年是相似的,但是一些突出显示的术语可能略有不同

答案 1 :(得分:1)

这应该这样做,你可以创建一个改变变量的循环。

DECLARE @CommentToAdd varchar(255)
DECLARE @VariableTextOption varchar(255)
DECLARE @SQLString nvarchar(max)
SET @VariableTextOption = 'some random'
SET @CommentToAdd = '/* This is the header that says ' + @VariableTextOption+ CHAR(13) + 'stuff */ ' + CHAR(13)

- 字符串操作删除现有注释并将CREATE指令更改为ALTER

SET @SQLString =  (SELECT @CommentToAdd + 'ALTER ' + RIGHT(r.ROUTINE_DEFINITION, (LEN(r.ROUTINE_DEFINITION) - CHARINDEX('PROCEDURE', r.ROUTINE_DEFINITION, 0)+1)) FROM INFORMATION_SCHEMA.ROUTINES r WHERE r.ROUTINE_TYPE = 'PROCEDURE' and ROUTINE_NAME = 'test')
exec (@SQLString)

答案 2 :(得分:0)

问题已经很久了,已经得到了回答,但我认为我的回答要复杂得多,可能对将来的用户很方便。

在应用以下脚本之前,请记住要备份数据库。

-- declare variables
DECLARE @OBJ_COUNT INT,
    @COUNT INT = 0,
    @OBJ_NAME NVARCHAR(128),
    @OBJ_DESC NVARCHAR(128),
    @TEXT VARCHAR(MAX),  
    @FINAL_TEXT NVARCHAR(MAX)

-- declare table variables
DECLARE @HELPTEXT TABLE(Val VARCHAR(MAX))
DECLARE @OBJECTS TABLE(ID INT PRIMARY KEY IDENTITY, ObjName NVARCHAR(128), ObjDesc NVARCHAR(128))

-- insert into @@OBJECTS all objects you want to alter
INSERT INTO @OBJECTS
SELECT NAME, TYPE_DESC
FROM sys.all_objects
-- change below to SQL_STORED_PROCEDURE only if you do not want to add comment to functions and triggers
WHERE TYPE_DESC IN ('SQL_STORED_PROCEDURE','SQL_TRIGGER', 'SQL_SCALAR_FUNCTION', 'SQL_TABLE_VALUED_FUNCTION')
 -- include only user objects
AND is_ms_shipped = 0 
-- you can filter to apply changes only to few objects
--AND NAME LIKE '%MY_STORED_PROCEDURES%'

-- get objects count
SET @OBJ_COUNT = (SELECT COUNT(1) FROM @OBJECTS);

-- loop through objects 
WHILE (@count < @obj_count)
BEGIN
    -- increment 
    SET @COUNT = @COUNT + 1;
    SET @TEXT = N'';

    -- get current object name and type
    SELECT @OBJ_NAME = ObjName,
        @OBJ_DESC = ObjDesc
    FROM @OBJECTS 
    WHERE ID = @COUNT

    -- get current object description
    INSERT INTO @HELPTEXT
    EXEC sp_HelpText @OBJ_NAME;

    SELECT @TEXT = COALESCE(@TEXT + '' + Val, Val)
    FROM @HELPTEXT;

    --PRINT @TEXT       -- for debug purposes

    -- clear table
    DELETE FROM @HELPTEXT;

    -- alter object if has no comment already
    IF @TEXT NOT LIKE '%/*-----------------------------------------------------------------------------------%'
    BEGIN
        -- changed create to alter
        IF (@OBJ_DESC = 'SQL_STORED_PROCEDURE')
        BEGIN
            SET @TEXT = REPLACE(@TEXT, 'CREATE PROCEDURE', 'ALTER PROCEDURE')
        END
        ELSE IF (@OBJ_DESC = 'SQL_TRIGGER')
        BEGIN
            SET @TEXT = REPLACE(@TEXT, 'CREATE TRIGGER', 'ALTER TRIGGER')
        END
        ELSE -- one of two types of function ('SQL_SCALAR_FUNCTION', 'SQL_TABLE_VALUED_FUNCTION')
        BEGIN
            SET @TEXT = REPLACE(@TEXT, 'CREATE FUNCTION', 'ALTER FUNCTION')
        END

        SET @text = 
'/*-----------------------------------------------------------------------------------
Name        :   ' + @OBJ_NAME + '
--------------------------------------------------------------------------------------
--- UPDATED BY --- UPDATED DATE --- DESCRIPTION
---                                                                                                                 
-----------------------------------------------------------------------------------*/'
+ CHAR(13) + 
@TEXT

        -- initially @TEXT is VARCHAR(MAX) to handle huge objects, but to sp_executesql we must pass NTEXT, NCHAR or NVARCHAR only,
        -- so we need to case in below line...
        SET @FINAL_TEXT = CAST(@TEXT AS NVARCHAR(MAX))
        --select @final_text    -- for debug purposes
        --print @obj_name       -- for debug purposes

        -- final update/alter object
        EXECUTE sp_executesql @FINAL_TEXT
    END
END