C. Gabriel
C. Gabriel

Reputation: 661

How can I find what stored procedures does a custom role have execute permission on? SQL SEVER

I am trying to identify on which stored procedures a custom role has execute permission using a query. I tried using has_perms_by_name, but I failed to understand and use it.

Upvotes: 0

Views: 791

Answers (2)

Jeroen Mostert
Jeroen Mostert

Reputation: 28809

Assuming you only care what stored procedures the role has been explicitly granted execute permissions on:

DECLARE @role SYSNAME = 'MyRole';

SELECT o.[name]
FROM sys.database_permissions p
JOIN sys.objects o ON p.major_id = o.[object_id]
JOIN sys.database_principals pr ON p.grantee_principal_id = pr.principal_id
WHERE pr.[name] = @role
    AND p.[state] = 'G' -- GRANT
    AND p.[type] = 'EX' -- EXECUTE
    AND o.[type] = 'P' -- PROCEDURE

This does not cover the (less common) case where the role has been given a global GRANT EXECUTE.

Upvotes: 1

B3S
B3S

Reputation: 1051

this should give your target:

DECLARE @Obj_sql VARCHAR(2000)
DECLARE @Obj_table TABLE (DBName VARCHAR(200), UserName VARCHAR(250), ObjectName VARCHAR(500), Permission VARCHAR(200), objecttype varchar(200))
SET @Obj_sql='select ''?'' as DBName,U.name as username, O.name as object,  permission_name as permission, o.type from ?.sys.database_permissions
join ?.sys.sysusers U on grantee_principal_id = uid join ?.sys.sysobjects O on major_id = id WHERE ''?'' NOT IN (''master'',''msdb'',''model'',''tempdb'') order by U.name '

INSERT @Obj_table
EXEC sp_msforeachdb @command1=@Obj_sql

SELECT * FROM @Obj_table
where UserName = 'RSExecRole' --edit with username you're looking for
and objecttype = 'P'

Upvotes: 0

Related Questions