:::: MENU ::::

get Database ID and Database Name without any join to sys.databases

  • Feb 19 / 2009
  • 0
dbDigger, System Functions, T-SQL Tips and Tricks

get Database ID and Database Name without any join to sys.databases

Some times it is required to get database ID to be used in a script. Or it is required to get database name from database ID. One approach is to make a join with sys.database. But i want to point out a very handy function for these tasks.

To get database ID from database name use following command

SELECT DB_ID(‘AdventureWorks’)
GO

And similarly to get database name from database ID use following command

SELECT DB_NAME(5)
GO

So these make the tasks easy.

Consult us to explore the Databases. Contact us