SQL Flow Field Guide

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

  1. Record SUSER_SNAME, ORIGINAL_LOGIN, and USER_NAME. They answer different questions about the current server login and database principal.
  2. Read HAS_PERMS_BY_NAME for the exact database securable and permission. A result of NULL means the check did not establish a yes or no answer.
  3. Read the permissions returned by sys.fn_my_permissions for the caller's current database security context. Compare the exact task with the permission name and scope.
  4. Repeat after an approved role change in a separate review. Do not use GRANT, DENY, REVOKE, or EXECUTE AS in this read-only exercise.
  5. 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 Flow
SELECT 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