How to encrypt all existing stored procedures of a database

前端 未结 8 1958
后悔当初
后悔当初 2021-02-10 10:13

Is there any possibility to encrypt all existing stored procedures of a SQL Server 2008 database AFTER they have been created via an SQLCMD script?

The reason I want to

8条回答
  •  忘了有多久
    2021-02-10 10:46

    I would recommend creating the sproc in a multi-line string variable and then inserting or altering it using sp_executesql. The only annoying downside to this approach is doubling of single quotes for strings.

    DECLARE @action varchar(max);
    SET @action = 'CREATE'; /* or "ALTER" */
    
    DECLARE @withEncryption varchar(max);
    SET @withEncryption = ''; /* or "WITH ENCRYPTION" */
    
    DECLARE @sql varchar(max);
    SET @sql = @action + ' PROCEDURE dbo.Something'
        (
            ....
        ) ' + @withEncryption +
        ' AS
        BEGIN
            DECLARE @bob varchar(10);
            SET @bob = ''Bob'';
            ....
        END;
        ';
    
    EXEC sp_executesql @statement = @sql;
    

    [Note the whitespace around the variables.]

    All of my scripts use this method, which works well once you get used to the quote doubling thing.

    I also use a batch file to call the script, and SQLCMD-mode command line variables to select various behaviours, which makes it repeatable and easy to test.

提交回复
热议问题