Events
Mar 31, 11 PM - Apr 2, 11 PM
The biggest SQL, Fabric and Power BI learning event. March 31 – April 2. Use code FABINSIDER to save $400.
Register todayThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Applies to:
SQL Server
Azure SQL Database
Azure SQL Managed Instance
Azure Synapse Analytics
Analytics Platform System (PDW)
This function returns the name of a specified database.
Transact-SQL syntax conventions
DB_NAME ( [ database_id ] )
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 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];
GO
This example returns the database name for database ID 3
.
USE master;
GO
SELECT DB_NAME(3) AS [Database Name];
GO
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;
DB_ID (Transact-SQL)
Metadata Functions (Transact-SQL)
sys.databases (Transact-SQL)
Events
Mar 31, 11 PM - Apr 2, 11 PM
The biggest SQL, Fabric and Power BI learning event. March 31 – April 2. Use code FABINSIDER to save $400.
Register today