Getting Database Name using ID and vice versa using SQL Server

In this blog, we see how to get Database Name using ID and vice versa using SQL Server


Syntax:

-- Get Name from ID.

SELECT DB_NAME('DatabaseID') AS [DatabaseName from ID];

 

-- Get ID from Name.

SELECT DB_ID('DatabaseName')) AS [DatabaseID from NAME];


Usage:

-- Get Name from ID.

SELECT DB_NAME(DB_ID()) AS [DatabaseName from ID];

 

-- Get ID from Name.

SELECT DB_ID(DB_NAME()) AS [DatabaseID from NAME];


OUTPUT:


DatabaseName from ID

------------------------

Northwind

 

(1 row(s) affected)

 

DatabaseID from NAME

--------------------

26

 

(1 row(s) affected)


Thanks for reading :)


Ebook Download
View all
Learn
View all