How to encrypt all existing stored procedures of a database

前端 未结 8 1959
后悔当初
后悔当初 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:52

    1) I export Create code for SP and functions. Keep it backed up. for example D:\SP2.sql"

    2) this transact SQL code, generate the script to delete existing sP & Functions

    SELECT 'DROP PROCEDURE  [' + SCHEMA_NAME(p.schema_id) + '].[' + p.NAME + ']'  as A
    FROM sys.procedures p
    union
    SELECT  'DROP FUNCTION ' + [name]  
    FROM sysobjects WHERE [type] IN (N'FN', N'IF', N'TF', N'FS', N'FT') AND category = 0
    order by a
    

    3) This Poweshell code replace

    AS
    BEGIN
    

    by

    WITH ENCRYPTION 
    AS
    BEGIN
    

    The code

    $File = "D:\SP2.sql"
    $File2 = $File.Replace("SP2.sql","SP-WithEncrypt.sql")
    $sortie=""
    $SP = get-content -path $file
    echo $SP.Count
    For ($i = 0 ; $i -le $SP.Count)
    { if ($sp[$i] -eq "AS" -and $sp[$i+1] -eq "BEGIN")
       { $AEcrire = "`nWITH ENCRYPTION `n AS `n BEGIN"
       $i+=1 
              }
       else
       {$AEcrire =$sp[$i]
       }
       $sortie += "`n$AEcrire"
    
     $i+=1 
     $SP.Count-$i
    }
    
    $sortie| out-file $File2
    

    Would be faster with a .replace( ,), but problem with End of lines...

    4) run the SP-WithEncrypt.sql in SSMS

提交回复
热议问题