Any suggestions? Export (0) Print
Expand All

USER_NAME (Transact-SQL)

 

THIS TOPIC APPLIES TO: yesSQL Server (starting with 2008) yesAzure SQL Database yesAzure SQL Data Warehouse yesParallel Data Warehouse

Returns a database user name from a specified identification number.

Topic link icon Transact-SQL Syntax Conventions


USER_NAME ( [ id ] )
-- Azure SQL Data Warehouse and Parallel Data Warehouse
USER_NAME ( [ id ] )

id

Is the identification number associated with a database user. id is int. The parentheses are required.

nvarchar(256)

When id is omitted, the current user in the current context is assumed. If the parameter contains the word NULL will return NULL.When USER_NAME is called without specifying an id after an EXECUTE AS statement, USER_NAME returns the name of the impersonated user. If a Windows principal accesses the database by way of membership in a group, USER_NAME returns the name of the Windows principal instead of the group.

The following example returns the user name for user ID 13.

SELECT USER_NAME(13);
GO

The following example finds the name of the current user without specifying an ID.

SELECT USER_NAME();
GO

Here is the result set for a user that is a member of the sysadmin fixed server role.

------------------------------

dbo

(1 row(s) affected)

The following example finds the row in sysusers in which the name is equal to the result of applying the system function USER_NAME to user identification number 1.

SELECT name FROM sysusers WHERE name = USER_NAME(1);
GO

Here is the result set.

name

------------------------------

dbo

(1 row(s) affected)

The following example shows how USER_NAME behaves during impersonation.

SELECT USER_NAME();
GO
EXECUTE AS USER = 'Zelig';
GO
SELECT USER_NAME();
GO
REVERT;
GO
SELECT USER_NAME();
GO

Here is the result set.

DBO

Zelig

DBO

The following example returns the user name for user ID 13.

SELECT USER_NAME(13);

The following example finds the name of the current user without specifying an ID.

SELECT USER_NAME();

Here is the result set for a currently logged-in user.

Here is the result set.

------------------------------ 
User7                            

The following example finds the row in sysusers in which the name is equal to the result of applying the system function USER_NAME to user identification number 1.

SELECT name FROM sysusers WHERE name = USER_NAME(1);

Here is the result set.

name                           
------------------------------ 
User7                            

Community Additions

ADD
Show:
© 2016 Microsoft