How to encrypt all existing stored procedures of a database

前端 未结 8 1954
后悔当初
后悔当初 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 11:01

    I wrote a cursor, steps through and encrypts most objects.

                                DECLARE cur_ENCRYPT_ANTHING CURSOR READ_ONLY
                                FOR
                                        SELECT  STUFF(src.definition,
                                                      CASE WHEN CHARINDEX('AS' + CHAR(13),src.definition,1) = 0
                                                           THEN CASE WHEN CHARINDEX('AS ' + CHAR(13),src.definition,1) = 0 THEN CHARINDEX('AS ',src.definition,1)
                                                                     ELSE CHARINDEX('AS ' + CHAR(13),src.definition,1)
                                                                END
                                                           ELSE CHARINDEX('AS' + CHAR(13),src.definition,1)
                                                      END,3,'WITH ENCRYPTION AS' + CHAR(13))
                                        FROM    (SELECT o.name
                                                 ,      STUFF(RIGHT(sm.definition,LEN(sm.definition) - CHARINDEX('CREATE ',sm.definition,1) + 1),1,6,'ALTER') AS definition
                                                 FROM   sys.sql_modules AS sm
                                                        JOIN sys.objects AS o ON sm.object_id = o.object_id
                                                 WHERE  CAST(CASE WHEN sm.definition IS NULL THEN 1
                                                                  ELSE 0
                                                             END AS BIT) = 0
                                                        AND type <> 'TR'
                                                ) AS src
    
    
    
    
    
    
    
    
                                DECLARE @VLS NVARCHAR(MAX)
                                OPEN cur_ENCRYPT_ANTHING
    
                                FETCH NEXT FROM cur_ENCRYPT_ANTHING INTO @VLS
                                WHILE (@@fetch_status <> -1)
                                      BEGIN
                                            IF (@@fetch_status <> -2)
                                               BEGIN
                                                     BEGIN TRY
                                                           EXEC (@VLS)
    
                                                     END TRY
                                                     BEGIN CATCH
                                                           PRINT ERROR_MESSAGE()
                                                           PRINT ''
    
                                                           PRINT @VLS
                                                     END CATCH
                                               END
                                            FETCH NEXT FROM cur_ENCRYPT_ANTHING INTO @VLS
                                      END
    
                                CLOSE cur_ENCRYPT_ANTHING
                                DEALLOCATE cur_ENCRYPT_ANTHING
    

提交回复
热议问题