Skip to content

Instantly share code, notes, and snippets.

@jrdmb
Last active August 29, 2015 14:18
Show Gist options
  • Save jrdmb/b41cfa2b74a6ddc0f7e1 to your computer and use it in GitHub Desktop.
Save jrdmb/b41cfa2b74a6ddc0f7e1 to your computer and use it in GitHub Desktop.
For the current database, query permissions to objects (show all objects even if no permissions)
--All relevant db objects and permissions
;With CTE As
(
select o.name, o.type, o.type_desc, p.permission_name, p.state_desc, p.state, u.name as 'users'
from sys.objects o
left outer join sys.database_permissions p On o.object_id = p.major_id
left outer JOIN sys.sysusers u ON p.grantee_principal_id = u.uid
where o.type Not In ('D', 'F', 'IT', 'PK', 'S', 'SQ', 'UQ')
)
select distinct name, type, type_desc, permission_name, state_desc, state,
stuff((
SELECT ', ' + users
FROM CTE
WHERE name = c.Name
FOR XML PATH(''),TYPE).value('.','NVARCHAR(2000)'),1,1,'') AS 'users'
From CTE c
order by type, name, state
--Example output:
--udf_DateAsInt FN SQL_SCALAR_FUNCTION EXECUTE GRANT G archiveUser
--udf_FormatLinkValueString FN SQL_SCALAR_FUNCTION EXECUTE GRANT G archiveUser
--udf_FormatMultipleSelections FN SQL_SCALAR_FUNCTION EXECUTE GRANT G archiveUserRole
--udf_iElementsDateAsInt FN SQL_SCALAR_FUNCTION EXECUTE GRANT G archiveUser
#tsql
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment