Skip to content

Instantly share code, notes, and snippets.

@mvillegascuellar
Last active July 10, 2018 18:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mvillegascuellar/e6c5597a56d9f42f3bb81284ba9a987f to your computer and use it in GitHub Desktop.
Save mvillegascuellar/e6c5597a56d9f42f3bb81284ba9a987f to your computer and use it in GitHub Desktop.
-- roles
SELECT
us.name as userName,
us.authentication_type_desc,
'Role' as [PermissionType],
CAST(rol.name COLLATE Latin1_General_CI_AS_KS_WS as nvarchar(128)) as [Permission/Role],
CAST('' COLLATE Latin1_General_CI_AS_KS_WS as nvarchar(60)) as [class],
CAST('' COLLATE Latin1_General_CI_AS_KS_WS as nvarchar(60)) as [type],
CAST(null as sysname) as [ObjectName]
FROM
sys.database_principals us
INNER JOIN sys.database_role_members rom
ON rom.member_principal_id = us.principal_id
INNER JOIN sys.database_principals rol
ON rom.role_principal_id = rol.principal_id
UNION
-- securables
SELECT
us.name as userName,
us.authentication_type_desc,
'Securable' as [PermissionType],
per.permission_name,
per.class_desc,
obj.type_desc,
ISNULL(obj.name, tt.name)
FROM
sys.database_principals us
INNER JOIN sys.database_permissions per
ON us.principal_id = per.grantee_principal_id
LEFT JOIN sys.objects obj
ON per.major_id = obj.object_id
LEFT JOIN sys.table_types tt
ON per.major_id = tt.user_type_id
ORDER BY 1,2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment