AppDividend
Latest Code Tutorials

SQL USER_NAME Function Example

0

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

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.

Syntax

USER_NAME (Identification_Number);

Parameters

Identification_Number: The identification number associated with the user.

Note

  1. Identification_Number is entirely optional.
  2. If Identification_Number is provided within the function, then it will return the name of the user associated with that number.
  3. 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.

Works On

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.

Example

SELECT USER_NAME();

Output

‘db_user’

Explanation

Here, the identification number was not provided inside the function. So, it returned the name of the current user i.e., db_user.

Query 2

SELECT USER_NAME (10);

Output

‘Anmol’

Explanation

Here, the Identification number was provided as an input to the function. So, ‘Anmol,’ whose identification number was 10, is returned.

See also

SQL SESSION_USER

SQL SYSTEM_USER

SQL Insert Query

SQL Insert Multiple Rows

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.