T-SQL USER_NAME() Function
The T-SQL (Transact-SQL) USER_NAME() function returns a database user name from a specified identification number.
When the identification number is omitted, the current user in the current context is returned. If NULL is passed as the argument, NULL is returned.
Syntax
USER_NAME(id)
Parameters
id |
Optional. Specify the identification number associated with a database user. |
Return Value
Returns the database user name from a specified identification number.
Example:
The example below shows the usage of USER_NAME() function.
SELECT USER_NAME();
The above query will return the name of current user in the SQL Server database. Therefore if the SQL Server account that was used was 'za108909', it will return the following:
SELECT USER_NAME(); Result: 'za108909'
Similarly, if the SQL Server account associated with identification number 1 is 'dbo', the below query will return the following:
SELECT USER_NAME(1); Result: 'dbo'
❮ T-SQL Functions