Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding stored procedures having execute permission

I am using SQL Server 2008 R2. I need to list out all the stored procedures that a database user (MYUSER) has execute permission.

Also, I need to list out which are the stored procedures that the user does NOT have EXECUTE permission - but can read the script of the stored procedure

Is there any SQL statement or helper function for these purpose?

REFERENCE:

  1. Granting execute permission on all stored procedures in a certain database
like image 610
LCJ Avatar asked Oct 31 '12 06:10

LCJ


People also ask

How do I check for permissions on a stored procedure?

From Stored Procedure Properties, select the Permissions page. To grant permissions to a user, database role, or application role, select Search. In Select Users or Roles, select Object Types to add or clear the users and roles you want. Select Browse to display the list of users or roles.

How do I check permissions to run?

Use [SP_Config] select name, has_perms_by_name(name, 'OBJECT', 'EXECUTE') as has_execute from sys. procedures where name = 'proc_putObjectTVP'; But running this seems to give value 1 even if i remove the EXECUTE permission manually on the database. >_<

How do I grant a stored procedure execution permission?

Connect to the database. Click the Users & Groups folder, and locate the user you want to grant permissions to. Right-click the user, and select Copy from the popup menu. Locate the procedure you want to allow the user to execute, in the Stored Procedures folder.


2 Answers

Use HAS_PERMS_BY_NAME:

select name, 
    has_perms_by_name(name, 'OBJECT', 'EXECUTE') as has_execute,
    has_perms_by_name(name, 'OBJECT', 'VIEW DEFINITION') as has_view_definition
from sys.procedures
like image 116
Remus Rusanu Avatar answered Sep 20 '22 19:09

Remus Rusanu


To check the permission for a different user, use this:

use my_db;
EXECUTE AS user = 'my_user'
SELECT SUSER_NAME(), USER_NAME();
select name, 
    has_perms_by_name(name, 'OBJECT', 'EXECUTE') as has_execute

from sys.procedures
where name = 'myprocname';
revert;

Works for my SQL Server 2012.

like image 27
knb Avatar answered Sep 20 '22 19:09

knb