DB_NAME (Transact-SQL)

THIS TOPIC APPLIES TO: yesSQL Server (starting with 2008)yesAzure SQL DatabaseyesAzure SQL Data Warehouse yesParallel Data Warehouse

Returns the database name.

Topic link icon Transact-SQL Syntax Conventions


DB_NAME ( [ database_id ] )  


Is the identification number (ID) of the database to be returned. database_id is int, with no default. If no ID is specified, the current database name is returned.

Return types



If the caller of DB_NAME is not the owner of the database and the database is not master or tempdb, the minimum permissions required to see the corresponding row are ALTER ANY DATABASE or VIEW ANY DATABASE server-level permission, or CREATE DATABASE permission in the master database. The database to which the caller is connected can always be viewed in sys.databases.


By default, the public role has the VIEW ANY DATABASE permission, allowing all logins to see database information. To block a login from the ability to detect a database, REVOKE the VIEW ANY DATABASE permission from public, or DENY the VIEW ANY DATABASE permission for individual logins.


A. Returning the current database name

The following example returns the name of the current database.

SELECT DB_NAME() AS [Current Database];  

B. Returning the database name of a specified database ID

The following example returns the database name for database ID 3.

USE master;  
SELECT DB_NAME(3)AS [Database Name];  

Examples: Azure SQL Data Warehouse and Parallel Data Warehouse

C. Return the current database name

SELECT DB_NAME() AS [Current Database];  

D. Return the name of a database by using the database ID

The following example returns the database name and database_id for each database.

SELECT DB_NAME(database_id) AS [Database], database_id  
FROM sys.databases;  

See also

DB_ID (Transact-SQL)
Metadata Functions (Transact-SQL)
sys.databases (Transact-SQL)