/*
-----------------------------------------------------------------------------------------------------------------------
名 稱:
調用對像:
備註說明:
修改日志:
程式作者:小曹
-----------------------------------------------------------------------------------------------------------------------
*/
CREATE
PROCEDURE
[
dbo
]
.
[
UP_DECRYPT
]
(
@OBJECTNAME
VARCHAR
(
50
)
)
AS
BEGIN
SET
NOCOUNT
ON
BEGIN
TRAN
DECLARE
@OBJECTNAME1
VARCHAR
(
100
),
@ORGVARBIN
VARBINARY
(
8000
)
DECLARE
@SQL1
NVARCHAR
(
4000
),
@SQL2
VARCHAR
(
8000
),
@SQL3
NVARCHAR
(
4000
),
@SQL4
NVARCHAR
(
4000
)
DECLARE
@ORIGSPTEXT1
NVARCHAR
(
4000
),
@ORIGSPTEXT2
NVARCHAR
(
4000
) ,
@ORIGSPTEXT3
NVARCHAR
(
4000
),
@RESULTSP
NVARCHAR
(
4000
)
DECLARE
@I
INT
,
@STATUS
INT
,
@TYPE
VARCHAR
(
10
),
@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
'
BEGIN
DECLARE
@PARENT_OBJ
VARCHAR
(
255
),
@TR_PARENT_XTYPE
VARCHAR
(
10
)
SELECT
@PARENT_OBJ
=
PARENT_OBJ
FROM
SYSOBJECTS
WHERE
ID
=
OBJECT_ID
(
@OBJECTNAME
)
SELECT
@TR_PARENT_XTYPE
=
XTYPE
FROM
SYSOBJECTS
WHERE
ID
=
@PARENT_OBJ
IF
@TR_PARENT_XTYPE
=
'
V
'
BEGIN
SET
@SQL1
=
'
ALTER TRIGGER
'
+
@OBJECTNAME
+
'
ON
'
+
OBJECT_NAME
(
@PARENTID
)
+
'
WITH ENCRYPTION INSTERD OF INSERT AS PRINT 1
'
END
ELSE
BEGIN
SET
@SQL1
=
'
ALTER TRIGGER
'
+
@OBJECTNAME
+
'
ON
'
+
OBJECT_NAME
(
@PARENTID
)
+
'
WITH ENCRYPTION FOR INSERT AS PRINT 1
'
END
END
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(10)) 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 END
'
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 AS F
'
SET
@Q
=
LEN
(
@SQL1
)
SET
@SQL1
=
@SQL1
+
REPLICATE
(
'
-
'
,
4000
-
@Q
)
SELECT
@SQL2
=
REPLICATE
(
'
-
'
,
8000
)
SET
@SQL3
=
'
EXEC(@SQL1
'
SELECT
@COLID
=
MAX
(COLID)
FROM
#
TEMP
WHERE
NUMBER
=
@K
SET
@N
=
1
WHILE
@N
<=
CEILING
(
1.0
*
(
@COLID
-
1
)
/
2
)
AND
LEN
(
@SQL3
)
<=
3996
BEGIN
SET
@SQL3
=
@SQL3
+
'
+@
'
SET
@N
=
@N
+
1
END
SET
@SQL3
=
@SQL3
+
'
)
'
EXEC
SP_EXECUTESQL
@SQL3
,N
'
@SQL1 NVARCHAR(4000),@ VARCHAR(8000)
'
,
@SQL1
=
@SQL1
,@
=
@SQL2
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
'
SET
@ORIGSPTEXT2
=
(
CASE
@TYPE
WHEN
'
TF
'
THEN
'
CREATE FUNCTION
'
+
@OBJECTNAME
+
'
(@A CHAR(1)) RETURNS @B TABLE(A VARCHAR(10)) 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 END
'
WHEN
'
IF
'
THEN
'
CREATE FUNCTION
'
+
@OBJECTNAME
+
'
(@A CHAR(1)) RETURNS TABLE WITH ENCRYPTION AS RETURN SELECT @A AS A
'
END
)
IF
@TYPE
=
'
TR
'
BEGIN
IF
@TR_PARENT_XTYPE
=
'
V
'
BEGIN
SET
@ORIGSPTEXT2
=
'
CREATE TRIGGER
'
+
@OBJECTNAME
+
'
ON
'
+
OBJECT_NAME
(
@PARENTID
)
+
'
WITH ENCRYPTION INSTEAD OF INSERT AS PRINT 1
'
END
ELSE
BEGIN
SET
@ORIGSPTEXT2
=
'
CREATE TRIGGER
'
+
@OBJECTNAME
+
'
ON
'
+
OBJECT_NAME
(
@PARENTID
)
+
'
WITH ENCRYPTION FOR INSERT AS PRINT 1
'
END
END
IF
@TYPE
=
'
V
'
SET
@ORIGSPTEXT2
=
'
CREATE VIEW
'
+
@OBJECTNAME
+
'
WITH ENCRYPTION AS SELECT 1 AS F
'
SET
@Q
=
4000
-
LEN
(
@ORIGSPTEXT2
)
SET
@ORIGSPTEXT2
=
@ORIGSPTEXT2
+
REPLICATE
(
'
-
'
,
@Q
)
END
ELSE
BEGIN
SET
@ORIGSPTEXT2
=
REPLICATE
(
'
-
'
,
4000
)
END
SET
@I
=
1
SET
@RESULTSP
=
REPLICATE
(N
'
A
'
, (
DATALENGTH
(
@ORIGSPTEXT1
)
/
2
))
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
SET
@N
=
@N
+
1
END
END
SET
@K
=
@K
+
1
END
DROP
TABLE
#
TEMP
ROLLBACK
TRAN
END
GO