Skip to content

Latest commit

 

History

History
68 lines (53 loc) · 3.04 KB

user-id-transact-sql.md

File metadata and controls

68 lines (53 loc) · 3.04 KB
title description author ms.author ms.date ms.service ms.subservice ms.topic f1_keywords helpviewer_keywords dev_langs
USER_ID (Transact-SQL)
USER_ID (Transact-SQL)
VanMSFT
vanto
03/14/2017
sql
t-sql
reference
USER_ID
USER_ID_TSQL
USER_ID function
identification numbers [SQL Server]
IDs [SQL Server], databases
users [SQL Server], database ID numbers
database IDs [SQL Server]
identification numbers [SQL Server], databases
TSQL

USER_ID (Transact-SQL)

[!INCLUDE SQL Server Azure SQL Database Azure SQL Managed Instance]

Returns the identification number for a database user.

Important

[!INCLUDEssNoteDepFutureAvoid] Use DATABASE_PRINCIPAL_ID instead.

:::image type="icon" source="../../includes/media/topic-link-icon.svg" border="false"::: Transact-SQL syntax conventions

Syntax

USER_ID ( [ 'user' ] )  

Arguments

user
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

int

Remarks

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, [!INCLUDEssNoVersion] 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).

Examples

The following example returns the identification number for the [!INCLUDE sssampledbobject-md] user Harold.

USE AdventureWorks2022;  
SELECT USER_ID('Harold');  
GO  

See Also

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