USER_ID (Transact-SQL)

Applies to: SQL Server (all supported versions) Azure SQL Database Azure SQL Managed Instance

Returns the identification number for a database user.


This feature will be removed in a future version of Microsoft SQL Server. Avoid using this feature in new development work, and plan to modify applications that currently use this feature. Use DATABASE_PRINCIPAL_ID instead.

Topic link icon Transact-SQL Syntax Conventions


USER_ID ( [ 'user' ] )  


To view Transact-SQL syntax for SQL Server 2014 and earlier, see Previous versions documentation.


Is the username to be used. user is nchar. If a char value is specified, it is implicitly converted to nchar. The parentheses are required.

Return Types



When user is omitted, the current user is assumed. If the parameter contains the word NULL will return NULL.When USER_ID is called after EXECUTE AS, USER_ID will return the ID of the impersonated context.

When a Windows principal that is not mapped to a specific database user accesses a database by way of membership in a group, USER_ID returns 0 (the ID of public). If such a principal creates an object without specifying a schema, SQL Server will create an implicit user and schema mapped to the Windows principal. The user created in such cases cannot be used to connect to the database. Calls to USER_ID by a Windows principal mapped to an implicit user will return the ID of the implicit user.

USER_ID can be used in a select list, in a WHERE clause, and anywhere an expression is allowed. For more information, see Expressions (Transact-SQL).


The following example returns the identification number for the AdventureWorks2012 user Harold.

USE AdventureWorks2012;  
SELECT USER_ID('Harold');  

See Also

USER_NAME (Transact-SQL)
sys.database_principals (Transact-SQL)
Security Functions (Transact-SQL)