| title | DB_NAME (Transact-SQL) | |||||
|---|---|---|---|---|---|---|
| description | The DB_NAME function returns the name of a specified database. | |||||
| author | VanMSFT | |||||
| ms.author | vanto | |||||
| ms.reviewer | randolphwest | |||||
| ms.date | 04/04/2025 | |||||
| ms.service | sql | |||||
| ms.subservice | t-sql | |||||
| ms.topic | reference | |||||
| ms.custom |
|
|||||
| f1_keywords |
|
|||||
| helpviewer_keywords |
|
|||||
| dev_langs |
|
|||||
| monikerRange | >=aps-pdw-2016 || =azuresqldb-current || =azure-sqldw-latest || >=sql-server-2016 || >=sql-server-linux-2017 || =azuresqldb-mi-current || =fabric || =fabric-sqldb |
[!INCLUDE sql-asdb-asdbmi-asa-pdw-fabricse-fabricdw-fabricsqldb]
This function returns the name of a specified database.
:::image type="icon" source="../../includes/media/topic-link-icon.svg" border="false"::: Transact-SQL syntax conventions
DB_NAME ( [ database_id ] )
The identification number (ID) of the database whose name DB_NAME returns. If the call to DB_NAME omits database_id, or the database_id is 0, DB_NAME returns the name of the current database.
nvarchar(128)
If the caller of DB_NAME doesn't own a specific non-master or non-tempdb database, ALTER ANY DATABASE or VIEW ANY DATABASE server-level permissions are required at minimum to see the corresponding DB_ID row.
For the master database, DB_ID needs CREATE DATABASE permission at minimum.
The database to which the caller connects always appears in sys.databases.
Important
By default, the public role has the VIEW ANY DATABASE permission, which allows all logins to see database information. To prevent a login from detecting a database, REVOKE the VIEW ANY DATABASE permission from public, or DENY the VIEW ANY DATABASE permission for individual logins.
This example returns the name of the current database.
SELECT DB_NAME() AS [Current Database];
GOThis example returns the database name for database ID 3.
USE master;
GO
SELECT DB_NAME(3) AS [Database Name];
GOExamples: [!INCLUDE ssazuresynapse-md] and [!INCLUDE ssPDW]
This example returns the current database name.
SELECT DB_NAME() AS [Current Database];This example returns the database name and database_id for each database.
SELECT DB_NAME(database_id) AS [Database],
database_id
FROM sys.databases;