【sql】加密所有的存储程式

因项目管理规定,所有的存储程式(SP)都需要加密。

如何批量加密所有的SP呢?

在网上找到了参考的代码,然后发现除SP外连同View,Trigger,Function等也可以一并处理!

如下是在原作基础上略作补充的方法:


create procedure sp_EncryptObject 
(
    @Object sysname='All'
)
as
/*
    当@Object=All的时候,对所有的函数,存储过程,视图和触发器进行加密
    调用方法:
    1. Execute sp_EncryptObject 'All'
    2. Execute sp_EncryptObject 'ObjectName'
*/
begin
    set nocount on
    
    if @Object <>'All'
    begin
        if not exists(select 1 from sys.objects a where a.object_id=object_id(@Object) And a.type in('P','V','TR','FN','IF','TF'))
        begin
            --SQL Server 2008
            --raiserror 50001 N'无效的加密对象!加密对象必须是函数,存储过程,视图或触发器。'

            --SQL Server 2012
            --throw 50001, N'无效的加密对象!加密对象必须是函数,存储过程,视图或触发器。',1  
            --SQL Server 2016
            raiserror ('无效的加密对象!加密对象必须是函数,存储过程,视图或触发器。',16,1)

            return
        end
        
        if exists(select 1 from sys.sql_modules a where a.object_id=object_id(@Object) and a.definition is null)
        begin
            --SQL Server 2008
            --raiserror 50001 N'对象已经加密!'

            --SQL Server 2012
            --throw 50001, N'对象已经加密!',1  

            --SQL Server 2016
            raiserror ('无效的加密对象!加密对象必须是函数,存储过程,视图或触发器。',16,1)

            return
        end
    end
    
    declare @sql nvarchar(max),@C1 nchar(1),@C2 nchar(1),@type nvarchar(50),@Replace nvarchar(50)
    set @C1=nchar(13)
    set @C2=nchar(10)
    
    
    declare cur_Object 
        cursor for 
            select object_name(a.object_id) As ObjectName,a.definition 
                from sys.sql_modules a  
                    inner join sys.objects b on b.object_id=a.object_id
                        and b.is_ms_shipped=0
                        and not exists(select 1 
                                            from sys.extended_properties x
                                            where x.major_id=b.object_id
                                                and x.minor_id=0
                                                and x.class=1
                                                and x.name='microsoft_database_tools_support'
                                        )
                where b.type in('P','V','TR','FN','IF','TF')
                    and (b.name=@Object or @Object='All')
                    --and ( b.name like'%abc_%' or b.name like'%_20240820%'  )
                    and b.name <>'sp_EncryptObject'
                    and a.definition is not null                    
                order by Case 
                            when b.type ='V' then 1 
                            when b.type ='TR' then 2
                            when b.type in('FN','IF','TF') then 3 
                            else 4 end,b.create_date,b.object_id
                
    open cur_Object
    fetch next from cur_Object into @Object,@sql
    while @@fetch_status=0
    begin
        Begin Try  
            if objectproperty(object_id(@Object),'ExecIsAfterTrigger')=0 set @Replace='As' ; else set @Replace='For ';
                
            if (patindex('%'+@C1+@C2+@Replace+@C1+@C2+'%',@sql)>0)
            begin
                set @sql=Replace(@sql,@C1+@C2+@Replace+@C1+@C2,@C1+@C2+'With Encryption'+@C1+@C2+@Replace+@C1+@C2)
            end
            else if(patindex('%'+@C1+@Replace+@C1+'%',@sql)>0)
            begin 
                set @sql=Replace(@sql,@C1+@Replace+@C1,@C1+'With Encryption'+@C1+@Replace+@C1)
            end
            else if(patindex('%'+@C2+@Replace+@C2+'%',@sql)>0)
            begin 
                set @sql=Replace(@sql,@C2+@Replace+@C2,@C2+'With Encryption'+@C2+@Replace+@C2)
            end
            else if(patindex('%'+@C2+@Replace+@C1+'%',@sql)>0)
            begin 
                set @sql=Replace(@sql,@C2+@Replace+@C1,@C1+'With Encryption'+@C2+@Replace+@C1)
            end
            else if(patindex('%'+@C1+@C2+@Replace+'%',@sql)>0)
            begin 
                set @sql=Replace(@sql,@C1+@C2+@Replace,@C1+@C2+'With Encryption'+@C1+@C2+@Replace)
            end
            else if(patindex('%'+@C1+@Replace+'%',@sql)>0)
            begin 
                set @sql=Replace(@sql,@C1+@Replace,@C1+'With Encryption'+@C1+@Replace)
            end
            else if(patindex('%'+@C2+@Replace+'%',@sql)>0)
            begin 
                set @sql=Replace(@sql,@C2+@Replace,@C2+'With Encryption'+@C2+@Replace)
            end
                    
            set @type =
                case 
                    when object_id(@Object,'P')>0 then 'Proc'
                    when object_id(@Object,'V')>0 then 'View'
                    when object_id(@Object,'TR')>0  then 'Trigger'
                    when object_id(@Object,'FN')>0 or object_id(@Object,'IF')>0 or object_id(@Object,'TF')>0 then 'Function'
                end
            set @sql=Replace(@sql,'Create '+@type,'Alter '+@type)
            
            Begin Transaction
            exec(@sql)            
            print N'已完成加密对象('+@type+'):'+@Object            
            Commit Transaction
            
        End Try
        Begin Catch
            Declare @Error nvarchar(2047)
            Set @Error='Object: '+@Object+@C1+@C2+'Error: '+Error_message()

            Rollback Transaction          
            print @Error
            print @sql   
        End Catch
                    
        fetch next from cur_Object into @Object,@sql
    end

    close cur_Object
    deallocate cur_Object        
end

Go
exec sp_ms_marksystemobject 'sp_EncryptObject' --标识为系统对象
go
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值