Grant execute permission for a user on all stored procedures in database?

前端 未结 5 911
傲寒
傲寒 2020-12-07 14:40

I generated script from old database, created a new database and imported all data from old database. So far so good, however, no user has execute rights for stored procedur

5条回答
  •  天涯浪人
    2020-12-07 14:49

    This is a solution that means that as you add new stored procedures to the schema, users can execute them without having to call grant execute on the new stored procedure:

    IF  EXISTS (SELECT * FROM sys.database_principals WHERE name = N'asp_net')
    DROP USER asp_net
    GO
    
    IF  EXISTS (SELECT * FROM sys.database_principals 
    WHERE name = N'db_execproc' AND type = 'R')
    DROP ROLE [db_execproc]
    GO
    
    --Create a database role....
    CREATE ROLE [db_execproc] AUTHORIZATION [dbo]
    GO
    
    --...with EXECUTE permission at the schema level...
    GRANT EXECUTE ON SCHEMA::dbo TO db_execproc;
    GO
    
    --http://www.patrickkeisler.com/2012/10/grant-execute-permission-on-all-stored.html
    --Any stored procedures that are created in the dbo schema can be 
    --executed by users who are members of the db_execproc database role
    
    --...add a user e.g. for the NETWORK SERVICE login that asp.net uses
    CREATE USER asp_net 
    FOR LOGIN [NT AUTHORITY\NETWORK SERVICE] 
    WITH DEFAULT_SCHEMA=[dbo]
    GO
    
    --...and add them to the roles you need
    EXEC sp_addrolemember N'db_execproc', 'asp_net';
    EXEC sp_addrolemember N'db_datareader', 'asp_net';
    EXEC sp_addrolemember N'db_datawriter', 'asp_net';
    GO
    

    Reference: Grant Execute Permission on All Stored Procedures

提交回复
热议问题