Audit effective permissions without changing access
Inspect the caller's login, database principal, and effective database permissions before requesting or reviewing a role change.
What you are trying to do
A developer needs to know whether the current connection can view database definitions. Run the read-only checks in the target SQL Server database under the same account that will perform the task.
Work through it
- Record
SUSER_SNAME,ORIGINAL_LOGIN, andUSER_NAME. They answer different questions about the current server login and database principal. - Read
HAS_PERMS_BY_NAMEfor the exact database securable and permission. A result of NULL means the check did not establish a yes or no answer. - Read the permissions returned by
sys.fn_my_permissionsfor the caller's current database security context. Compare the exact task with the permission name and scope. - Repeat after an approved role change in a separate review. Do not use GRANT, DENY, REVOKE, or EXECUTE AS in this read-only exercise.
- Request the narrow permission and scope needed for the work. Microsoft's function reference explains that effective permissions can come from roles and other memberships. sys.fn_my_permissions · HAS_PERMS_BY_NAME
Check your work
The report names the connection login and database user separately, reports the permission check result, and lists effective database permissions for the same connection. It changes no grants or execution context.
Practice SQL
Open in SQL FlowSELECT SUSER_SNAME() AS CurrentLogin, ORIGINAL_LOGIN() AS OriginalLogin, USER_NAME() AS DatabaseUser, HAS_PERMS_BY_NAME(DB_NAME(), 'DATABASE', 'VIEW DEFINITION') AS CanViewDefinitions;
SELECT permission_name
FROM sys.fn_my_permissions(NULL, 'DATABASE')
ORDER BY permission_name;
Knowledge check
Commands in this lesson
Next lesson Validate and shred a JSON feed