How to encrypt a SQL Server stored procedure
Here is an example of how to encrypt a SQL Server stored procedure. Before doing this, make sure your unencrypted version is stored in source control or somewhere else safe because once created with the "WITH ENCRYPTION" feature, you will no longer be able to access the version stored in SQL Server.
CREATE PROCEDURE dbo.MyStoredProcedure
WITH ENCRYPTION
AS
BEGIN
SELECT 'my data'
END
Applies to: SQL Server 2005/2008
Grammer of CREATE PROCEDURE
- Syntax
- CREATE PROC [ EDURE ] [ owner. ] procedure_name [ ; number ]
- [ { @parameter data_type }
- [ VARYING ] [ = default ] [ OUTPUT ]
- ] [ ,...n ]
- [ WITH
- { RECOMPILE | ENCRYPTION | RECOMPILE , ENCRYPTION } ]
- [ FOR REPLICATION ]
- AS sql_statement [ ...n ]
复制代码
Arguments
owner
Is the name of the user ID that owns the stored procedure. owner must be either the name of the current user or the name of a role that a current user is a member of.
procedure_name
Is the name of the new stored procedure. Procedure names must conform to the rules for identifiers and must be unique within the database and its owner. For more information, see Using Identifiers. Local or global temporary procedures can be created by preceding the procedure_name with a single number sign (#procedure_name) for local temporary procedures and a double number sign (##procedure_name) for global temporary procedures. The complete name, including # or ##, cannot exceed 128 characters. Specifying the procedure owner name is optional.
;number
Is an optional integer used to group procedures of the same name so they can be dropped together with a single DROP PROCEDURE statement. For example, the procedures used with an application called orders may be named orderproc;1, orderproc;2, and so on. The statement DROP PROCEDURE orderproc drops the entire group. If the name contains delimited identifiers, the number should not be included as part of the identifier; use the appropriate delimiter around procedure_name only.
{RECOMPILE | ENCRYPTION | RECOMPILE, ENCRYPTION}
RECOMPILE indicates that SQL Server does not cache a plan for this procedure and the procedure is recompiled at run time. Use the RECOMPILE option when using atypical or temporary values without overriding the execution plan cached in memory. ENCRYPTION indicates that SQL Server converts the original text of the CREATE PROCEDURE statement to an obfuscated format. Note that obfuscated stored procedures can be reverse engineered because SQL Server must de-obfuscate procedures for execution. In SQL Server 2000, the obfuscated text is visible in the syscomments system table and may be susceptible to de-obfuscation attempts. Using ENCRYPTION prevents the procedure from being published as part of SQL Server replication.
Note During an upgrade, SQL Server uses the obfuscated comments stored in syscomments to re-create obfuscated procedures. |