SQL USER_NAME Function Example
SQL USER_NAME is an inbuilt function that is used for returning the name of the user working currently in the SQL SERVER database by providing an identification number to the function. SQL USER_NAME returns the database user name (will return the current user since no id is specified).
SQL USER_NAME() function returns the database user name based on the specified id. If no id is specified, this function will return the name of the current user.
Identification_Number: The identification number associated with the user.
- Identification_Number is entirely optional.
- If Identification_Number is provided within the function, then it will return the name of the user associated with that number.
- If Identification_Number is not provided within the function, then it will return the current user name in the SQL SERVER in the present context.
It works on following SQL SERVER.
SQL Server 2017, SQL Server 2016, SQL Server 2014, SQL Server 2012, SQL Server 2008 R2, SQL Server 2008, SQL Server 2005.
Here, the identification number was not provided inside the function. So, it returned the name of the current user i.e., db_user.
SELECT USER_NAME (10);
Here, the Identification number was provided as an input to the function. So, ‘Anmol,’ whose identification number was 10, is returned.