Skip to content

Instantly share code, notes, and snippets.

@tcartwright
Created June 24, 2025 14:58
Show Gist options
  • Save tcartwright/aac2040634f2c0dfbf92cd3e8ec2a852 to your computer and use it in GitHub Desktop.
Save tcartwright/aac2040634f2c0dfbf92cd3e8ec2a852 to your computer and use it in GitHub Desktop.
SQL SERVER: Differences between user name functions
SELECT CURRENT_USER AS [CURRENT_USER], /*Returns the name of the current security context. If EXECUTE AS has been used, it returns the impersonated user's name. */
USER AS [USER], /*Similar to USER_NAME(), it returns the database user name and, after EXECUTE AS, the impersonated user. */
USER_NAME() AS [USER_NAME()], /*Returns the database user name based on the provided ID. If no ID is specified, it returns the current user's name. It also returns the impersonated user's name after EXECUTE AS. */
SESSION_USER AS [SESSION_USER], /*Returns the name of the user who initiated the session, even if EXECUTE AS has been used. */
SYSTEM_USER AS [SYSTEM_USER], /*Returns the name of the current security context. If EXECUTE AS has been used, it returns the impersonated user's name. */
SUSER_NAME() AS [SUSER_NAME()], /*Returns the login name. When called with a server principal ID, it returns the name associated with that ID. */
ORIGINAL_LOGIN() AS [ORIGINAL_LOGIN()] /*returns the login name of the user who initially connected to the session, regardless of any context switching. */
@tcartwright
Copy link
Author

My preference when I want the real user login name:

ORIGINAL_LOGIN() 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment