Created
June 24, 2025 14:58
-
-
Save tcartwright/aac2040634f2c0dfbf92cd3e8ec2a852 to your computer and use it in GitHub Desktop.
SQL SERVER: Differences between user name functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My preference when I want the real user login name: