DB_ID (Transact-SQL)

Applies to: SQL Server (all supported versions)

Returns the database ID for the specified database name.

Syntax

DB_ID ( N'database_name' ) 

Parameters

N'database_name'

Is the name of the database for which to return the database ID. database_name is sysname, which is the same as nvarchar(128). It is case-insensitive, unless the instance of SQL Server is running with a case-sensitive collation.

Return Value

Returns the database ID for the specified database name. If the database name is not valid, NULL is returned.

Permissions

Any user can execute DB_ID.

Examples

A. Returning the database ID for the current database

The following example returns the database ID for the current database.

SELECT DB_ID(DB_NAME());

B. Returning the database ID for the 'AdventureWorks2019' database

The following example returns the database ID for the AdventureWorks2019 database.

SELECT DB_ID(N'AdventureWorks2019');

See Also

Last updated: October 27, 2023