Skip to content

Instantly share code, notes, and snippets.

@ghotz
Created July 24, 2025 12:54
Show Gist options
  • Select an option

  • Save ghotz/44f51855d43a0f85b6fb2030cbfebbd2 to your computer and use it in GitHub Desktop.

Select an option

Save ghotz/44f51855d43a0f85b6fb2030cbfebbd2 to your computer and use it in GitHub Desktop.
Get detailed permissions for the specified roles
SELECT DISTINCT
RP.[name]
, RP.[type_desc] AS role_type
, PM.class_desc AS permission_type
, PM.[permission_name]
, pm.state_desc
, CASE
WHEN O1.type_desc IS NULL OR O1.type_desc = 'SYSTEM_TABLE'
THEN PM.class_desc
ELSE O1.[type_desc]
END AS object_type
, S2.[name] AS [schema_name]
, ISNULL(S1.[name], OBJECT_NAME(PM.major_id)) AS [object_name]
FROM sys.database_principals AS RP
JOIN sys.database_permissions AS PM
ON PM.grantee_principal_id = RP.principal_id
LEFT
JOIN sys.schemas AS S1
ON PM.major_id = S1.[schema_id]
LEFT
JOIN sys.objects AS O1
ON PM.[major_id] = O1.[object_id]
LEFT
JOIN sys.schemas AS S2
ON S2.[schema_id] = O1.[schema_id]
WHERE RP.type_desc = 'DATABASE_ROLE' --AND PM.class_desc <> 'DATABASE'
AND rp.name IN (N'', N'')
ORDER BY RP.[name], RP.[type_desc], PM.class_desc,[schema_name], [object_name]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment