How can I determine with TSQL which roles are provided and the execution permissions for a particular stored procedure?

How can I determine with TSQL which roles are provided and the execution permissions for a particular stored procedure? Is there a system stored procedure or system view that I can use?

+3
source share
2 answers

In 7.0 or 2000, you can change and use the following code:

SELECT convert(varchar(100),
        'GRANT ' +
        CASE         WHEN actadd & 32 = 32 THEN 'EXECUTE'
                ELSE
                        CASE WHEN actadd & 1 = 1   THEN 'SELECT' + CASE WHEN actadd & (8|2|16) > 0  THEN ', ' ELSE '' END ELSE '' END +
                        CASE WHEN actadd & 8 = 8   THEN 'INSERT' + CASE WHEN actadd & (2|16) > 0  THEN ', ' ELSE '' END ELSE '' END +
                        CASE WHEN actadd & 2 = 2   THEN 'UPDATE' + CASE WHEN actadd & (16) > 0  THEN ', ' ELSE '' END ELSE '' END +
                        CASE WHEN actadd & 16 = 16 THEN 'DELETE' ELSE '' END
        END + ' ON [' + o.name + '] TO [' + u.name + ']') AS '--Permissions--'
FROM syspermissions p
INNER JOIN sysusers u ON u.uid = p.grantee
INNER JOIN sysobjects o ON p.id = o.id
WHERE o.type <> 'S'
AND o.name NOT LIKE 'dt%'
--AND o.name = '<specific procedure/table>'
--AND u.name = '<specific user>'
ORDER BY u.name, o.name 
+1
source

You can try something like this. Notice I believe that 3 EXECUTE.

SELECT
grantee_principal.name AS [Grantee],
CASE grantee_principal.type WHEN 'R' THEN 3 WHEN 'A' THEN 4 ELSE 2 END - CASE 'database' WHEN  'database' THEN 0 ELSE 2 END AS [GranteeType]
FROM
sys.all_objects AS sp
INNER JOIN sys.database_permissions AS prmssn ON prmssn.major_id=sp.object_id AND prmssn.minor_id=0 AND prmssn.class=1
INNER JOIN sys.database_principals AS grantee_principal ON grantee_principal.principal_id = prmssn.grantee_principal_id
WHERE
(sp.type = N'P' OR sp.type = N'RF' OR sp.type='PC')and(sp.name=N'myProcedure' and SCHEMA_N

I got this example by simply using SQL Profiler when looking at the permissions of a procedure. Hope this helps.

0
source

Source: https://habr.com/ru/post/1696904/


All Articles