Returns the database identification number.
DB_ID(['database_name'])
smallint
This example examines each database in sysdatabases using the database name to determine the database identification number.
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)