I want to hide the script of a stored procedure in SQL Server 2008

后端 未结 4 1481
温柔的废话
温柔的废话 2021-02-15 14:27

I have written a stored procedure in SQL Server.

Now its just that I don\'t want any one to see my script or edit it.

Please remember that I am working on the a

相关标签:
4条回答
  • 2021-02-15 14:57

    Use the encryption keyword for your stored procedure. This will hide the code:

    CREATE PROCEDURE dbo.foo 
    WITH ENCRYPTION 
    AS 
    BEGIN 
        SELECT 'foo' 
    END
    
    0 讨论(0)
  • 2021-02-15 15:14

    SQL Server doesn't truly provide a foolproof method to protect module code. The WITH ENCRYPTION clause should be named something along the lines of WITH LOOSE_OBFUSCATION, since the "encryption" is very easily thwarted. You're not going to be able to use anything in SQL Server to make the code undecipherable by anyone except the most casual onlookers - anyone determined is going to be able to beat native methods without breaking a sweat.

    While this may be good enough for your needs, probably a safer way (but still not perfect) is to put some or all of the procedure's business logic into the CLR (read more here).

    0 讨论(0)
  • 2021-02-15 15:20

    You're looking for WITH ENCRYPTION, which encrypts the code behind your stored proc.

    CREATE PROCEDURE usp_MyProc
    WITH ENCRYPTION
    AS
    SELECT *
    FROM myTable
    

    Just a caveat, from MSDN:

    Users who have no access to system tables or database files cannot retrieve the obfuscated text. However, the text will be available to privileged users who can either access system tables over the DAC port or directly access database files.

    Some references and further reading:

    • http://blog.sqlauthority.com/2007/07/01/sql-server-explanation-of-with-encryption-clause-for-stored-procedure-and-user-defined-functions/
    • http://msdn.microsoft.com/en-us/library/ms187926.aspx
    0 讨论(0)
  • 2021-02-15 15:22

    Avoid using the WITH ENCRYPTION option other than for very specified requirements. It may lead to administrative problems later.

    Using WITH ENCRYPTION is not a recommended best practice to hide the definition/code of an object. Luckily, there is an alternative approach for SQL Server.

    If it is required to hide the definition/code of any object from a user then standard permissions can be used for this purpose. This can be done by granting or revoking View Definition rights.

    If permission View Definition is denied for an object to any user then the user would not be able to view the object in SSMS or view its code through the system stored procedure sp_helptext.

    The View Definition permission may also be used for other objects in SQL Server like tables, synonyms etc. View Definition permissions can be granted or denied using both T-SQL or SSMS

    For implementation of permissions through SSMS :

    1. Right click on the object that is required to hide the definition
    2. Click on Properties, a frame will appear, select grant or deny View Definition permission on the object for the selected user or role

    Denying View Definition permission will hide the object for a specific user and also the user will not be able to see the definition using sp_helptext

    Other permissions like SELECT, INSERT etc will remain intact.

    The permissions are flexible and can be implemented on the following four levels:

    1. Server level. At server level this permission will be listed as View Any Definition 2 .Database level
    2. Schema level
    3. Individual entity level

    Thank you hope it will clear confusion.

    0 讨论(0)
提交回复
热议问题