This function returns the name of a specified database.
Transact-SQL Syntax Conventions
database_id
The identification number (ID) of the database whose name DB_NAME will return. If the call to DB_NAME omits database_id, DB_NAME returns the name of the current database.
nvarchar(128)
If the caller of DB_NAME does not own a specific non-master or non-tempdb database, ALTER ANY DATABASE or VIEW ANY DATABASE server-level permissions at minimum are required 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 will always appear in sys.databases.
[!IMPORTANT]
By default, the public role has theVIEW ANY DATABASEpermission, which allows all logins to see database information. To prevent a login from detecting a database,REVOKEtheVIEW ANY DATABASEpermission from public, orDENYtheVIEW ANY DATABASEpermission for individual logins.
This example returns the name of the current database.
This example returns the database name for database ID 3.
This example returns the database name and database_id for each database.
DB_ID (Transact-SQL)
Metadata Functions (Transact-SQL)
sys.databases (Transact-SQL)