DB_NAME (Transact-SQL)

Applies to: yesSQL Server (all supported versions) YesAzure SQL Database YesAzure SQL Managed Example yesAzure Synapse Analytics yesAnalytics Platform System (PDW)

This part returns the proper noun of a specified database.

Topic link icon Transact-SQL Syntax Conventions

Syntax

              DB_NAME ( [ database_id ] )                          

Arguments

database_id

The identification number (ID) of the database whose name DB_NAME will render. If the call to DB_NAME omits database_id, DB_NAME returns the proper noun of the electric current database.

Render types

nvarchar(128)

Permissions

If the caller of DB_NAME does not own a specific not-master or not-tempdb database, ALTER ANY DATABASE or VIEW ANY DATABASE server-level permissions at minimum are required to run into 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 office has the VIEW Whatsoever DATABASE permission, which allows all logins to see database information. To prevent a login from detecting a database, REVOKE the VIEW Whatever DATABASE permission from public, or DENY the VIEW ANY DATABASE permission for individual logins.

Examples

A. Returning the current database name

This instance returns the name of the current database.

              SELECT DB_NAME() As [Electric current Database];   Get                          

B. Returning the database name of a specified database ID

This example returns the database name for database ID 3.

              Employ master;   Get   SELECT DB_NAME(three) AS [Database Name];   Become                          

Examples: Azure Synapse Analytics and Analytics Platform Organization (PDW)

C. Return the electric current database name

              SELECT DB_NAME() AS [Electric current Database];                          

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

This instance returns the database proper name and database_id for each database.

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

See besides

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