Use DB_ID() to Return the ID of a Database in SQL Server

In SQL Server, you can use the DB_ID() function to return the ID of the current database, or another specified database.

The way it works is, you pass the name of the database as an argument, and then the function will return the ID of that database. If you don’t pass a name it will return the ID of the current database.

Example 1 – Return the Current Database

Here’s a basic example to demonstrate how to return the name of the current database.

SELECT DB_ID() AS [Current Database];

Result:

+--------------------+
| Current Database   |
|--------------------|
| 6                  |
+--------------------+

In this case, the current database has an ID of 6.

Here’s another example that demonstrates it further, by switching databases.

USE Music;
SELECT DB_ID() AS [Current Database];

USE EMS;
SELECT DB_ID() AS [Current Database];

USE WideWorldImportersDW;
SELECT DB_ID() AS [Current Database];

Result:

Changed database context to 'Music'.
+--------------------+
| Current Database   |
|--------------------|
| 5                  |
+--------------------+
(1 row affected)
Changed database context to 'EMS'.
+--------------------+
| Current Database   |
|--------------------|
| 14                 |
+--------------------+
(1 row affected)
Changed database context to 'WideWorldImportersDW'.
+--------------------+
| Current Database   |
|--------------------|
| 6                  |
+--------------------+
(1 row affected)

Example 2 – Return a Specific Database

Here’s an example of returning a specific database. This is done by passing in the name of the database.

SELECT DB_ID('Music') AS Result;

Result:

+----------+
| Result   |
|----------|
| 5        |
+----------+

Here’s more:

SELECT 
  DB_ID('master') AS [master],
  DB_ID('tempdb') AS [tempdb],
  DB_ID('model') AS [model],
  DB_ID('msdb') AS [msdb],
  DB_ID('Music') AS [Music],
  DB_ID('WideWorldImportersDW') AS ['WideWorldImportersDW'];

Result:

+----------+----------+---------+--------+---------+--------------------------+
| master   | tempdb   | model   | msdb   | Music   | 'WideWorldImportersDW'   |
|----------+----------+---------+--------+---------+--------------------------|
| 1        | 2        | 3       | 4      | 5       | 6                        |
+----------+----------+---------+--------+---------+--------------------------+

Example 3 – A Database Query

Here’s an example of where DB_ID() could come in handy.

USE WideWorldImportersDW;
SELECT
  name AS [Foreign Key],
  OBJECT_NAME(parent_object_id, DB_ID('Music')) AS [Parent Object Name],
  OBJECT_NAME(referenced_object_id, DB_ID('Music')) AS [Referenced Object Name]
FROM Music.sys.foreign_keys
WHERE name = 'FK_Artists_Country';

Result:

Changed database context to 'WideWorldImportersDW'.
+--------------------+----------------------+--------------------------+
| Foreign Key        | Parent Object Name   | Referenced Object Name   |
|--------------------+----------------------+--------------------------|
| FK_Artists_Country | Artists              | Country                  |
+--------------------+----------------------+--------------------------+
(1 row affected)

In this example, the current database is WideWorldImportersDW, but I want information about a foreign key in the Music database. The OBJECT_NAME() function returns the object name based on its ID, but if the object is in a different database, it also allows me to specify the database ID. However, in this case I only know the database name. Not a problem though, because I can use DB_ID() to return the ID based on the name.