Rediger

Del via


DATABASE_PRINCIPAL_ID (Transact-SQL)

Applies to: SQL Server Azure SQL Database Azure SQL Managed Instance Azure Synapse Analytics

This function returns the ID number of a principal in the current database. See Principals (Database Engine) for more information about principals.

Transact-SQL syntax conventions

Syntax

DATABASE_PRINCIPAL_ID ( 'principal_name' )  

Arguments

principal_name
An expression of type sysname, that represents the principal. When principal_name is omitted, DATABASE_PRINCIPAL_ID returns the ID of the current user. DATABASE_PRINCIPAL_ID requires the parentheses.

Return types

int
NULL if the database principal does not exist.

Remarks

Use DATABASE_PRINCIPAL_ID in a select list, a WHERE clause, or any place that allows an expression. See Expressions (Transact-SQL) for more information.

Examples

A. Retrieving the ID of the current user

This example returns the database principal ID of the current user.

SELECT DATABASE_PRINCIPAL_ID();  
GO  

B. Retrieving the ID of a specified database principal

This example returns the database principal ID for the database role db_owner.

SELECT DATABASE_PRINCIPAL_ID('db_owner');  
GO  

See also

Principals (Database Engine)
Permissions Hierarchy (Database Engine)
sys.database_principals (Transact-SQL)