Copy Code code as follows:
Create PROCEDURE Sp_decrypt (@objectName varchar (50))
As
Begin
BEGIN Tran
DECLARE @objectname1 varchar (@orgvarbin), varbinary (8000)
declare @sql1 nvarchar (4000), @sql2 nvarchar (4000), @sql3 nvarchar (4000), @sql4 nvarchar (4000), @sql5 nvarchar (4000), @ SQL6 nvarchar (4000), @sql7 nvarchar (4000), @sql8 nvarchar (4000), @sql9 nvarchar (4000), @sql10 nvarchar (4000)
DECLARE @OrigSpText1 nvarchar (4000), @OrigSpText2 nvarchar (4000), @OrigSpText3 nvarchar (4000), @resultsp nvarchar (4000 )
DECLARE @i int, @status int, @type varchar (a), @parentid int
DECLARE @colid int,@n int,@q int,@j int,@k int, @encrypted int, @number int
Select @type =xtype, @parentid =parent_obj from sysobjects where id=object_id (@ObjectName)
CREATE TABLE #temp (number int,colid int,ctext varbinary (8000), encrypted int,status int)
Insert #temp SELECT number,colid,ctext,encrypted,status from syscomments WHERE id = object_id (@objectName)
Select @number =max (number) from #temp
Set @k=0
While @k<= @number
Begin
if exists (select 1 from syscomments where id=object_id (@objectname) and Number=@k)
Begin
If @type = ' P '
Set @sql1 = (case when @number >1 then ' ALTER PROCEDURE ' + @objectName + '; ') +rtrim (@k) + ' with encryption as '
Else ' ALTER PROCEDURE ' + @objectName + ' with encryption as '
End
If @type = ' TR '
Set @sql1 = ' ALTER TRIGGER ' + @objectname + ' on ' +object_name (@parentid) + ' and encryption for INSERT as PRINT 1 '
If @type = ' FN ' or @type = ' TF ' or @type = ' if '
Set @sql1 = (case @type when ' TF ' then
' ALTER FUNCTION ' + @objectName + ' (@a char (1)) Returns @b table (a varchar) with encryption as begin insert @b Select @a Return End '
When ' FN ' then
' ALTER FUNCTION ' + @objectName + ' (@a char (1)) returns char (1) with encryption as "begin return @a"
When ' IF ' then
' ALTER FUNCTION ' + @objectName + ' (@a char (1)) returns table with encryption as return select @a as a '
End
If @type = ' V '
Set @sql1 = ' ALTER VIEW ' + @objectname + ' with encryption as SELECT 1 '
Set @q=len (@sql1)
Set @sql1 = @sql1 +replicate ('-', 4000-@q)
Select @sql2 =replicate ('-', 4000), @sql3 =replicate ('-', 4000), @sql4 =replicate ('-', 4000), @sql5 =replicate ('-', 4000), @sql6 =replicate ('-', 4000), @sql7 =replicate ('-', 4000), @sql8 =replicate ('-', 4000), @sql9 =replicate ('-', 4000), @sql10 =replicate ('-', 4000)
EXEC (@sql1 + @sql2 + @sql3 + @sql4 + @sql5 + @sql6 + @sql7 + @sql8 + @sql9 + @sql10)
End
Set @k=@k+1
End
Set @k=0
While @k<= @number
Begin
if exists (select 1 from syscomments where id=object_id (@objectname) and Number=@k)
Begin
Select @colid =max (colid) from #temp where number=@k
Set @n=1
While @n<= @colid
Begin
Select @OrigSpText1 =ctext, @encrypted =encrypted, @status =status from #temp WHERE Colid=@n and Number=@k
SET @OrigSpText3 = (SELECT ctext from syscomments WHERE id=object_id (@objectName) and Colid=@n and Number=@k)
If @n=1
Begin
If @type = ' P '
SET @OrigSpText2 = (case when @number >1 then ' CREATE PROCEDURE ' + @objectName + '; ') +rtrim (@k) + ' with encryption as '
Else ' CREATE PROCEDURE ' + @objectName + ' with encryption as '
End
If @type = ' FN ' or @type = ' TF ' or @type = ' if '--a mistake just changed
SET @OrigSpText2 = (case @type when ' TF ' then
' CREATE FUNCTION ' + @objectName + ' (@a char (1)) Returns @b table (a varchar) with encryption as begin insert @b Select @a Return End '
When ' FN ' then
' CREATE FUNCTION ' + @objectName + ' (@a char (1)) returns char (1) with encryption as "begin return @a"
When ' IF ' then
' CREATE FUNCTION ' + @objectName + ' (@a char (1)) returns table with encryption as return select @a as a '
End
If @type = ' TR '
Set @OrigSpText2 = ' CREATE TRIGGER ' + @objectname + ' on ' +object_name (@parentid) + ' with encryption for INSERT as PRINT 1 '
If @type = ' V '
Set @OrigSpText2 = ' CREATE VIEW ' + @objectname + ' with encryption as SELECT 1 '
Set @q=4000-len (@OrigSpText2)
Set @OrigSpText2 = @OrigSpText2 +replicate ('-', @q)
End
Else
Begin
SET @OrigSpText2 =replicate ('-', 4000)
End
--start counter
SET @i=1
--fill Temporary variable
SET @resultsp = replicate (N ' A ', (datalength (@OrigSpText1)/2))
--loop
While @i<=datalength (@OrigSpText1)/2
BEGIN
SET @resultsp = Stuff (@resultsp, @i, 1, NCHAR (UNICODE (substring (@OrigSpText1, @i, 1)) ^
(UNICODE (substring (@OrigSpText2, @i, 1)) ^
UNICODE (substring (@OrigSpText3, @i, 1)))
SET @i=@i+1
End
Set @orgvarbin =cast (@OrigSpText1 as varbinary (8000))
Set @resultsp = (case when @encrypted =1
Then @resultsp
else convert (nvarchar (4000), case when @status &2=2 then uncompress (@orgvarbin) Else @orgvarbin end)
End
Print @resultsp
--execute (@resultsp)
Set @n=@n+1
End
End
Set @k=@k+1
End
drop table #temp
Rollback Tran
End
Go