DB_ID

Transact-SQL Reference

Transact-SQL Reference

DB_ID

Returns the database identification (ID) number.

Syntax

DB_ID ( [ 'database_name' ] )

Arguments

'database_name'

Is the database name used to return the corresponding database ID. database_name is nvarchar. If database_name is omitted, the current database ID is returned.

Return Types

smallint

Examples

This example examines each database in sysdatabases using the database name to determine the database ID.

USE master
SELECT name, DB_ID(name) AS DB_ID
FROM sysdatabases
ORDER BY dbid

Here is the result set:

name                           DB_ID  
------------------------------ ------ 
master                         1      
tempdb                         2      
model                          3      
msdb                           4      
pubs                           5      

(5 row(s) affected)

See Also

Metadata Functions