Skip to content

Commit cc22512

Browse files
Merge pull request #33714 from rwestMSFT/rw-0404-fix-10054
Add edge case when the parameter is 0 (PR 10054)
2 parents 737ec83 + 55a9db4 commit cc22512

File tree

1 file changed

+68
-55
lines changed

1 file changed

+68
-55
lines changed
Lines changed: 68 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
---
22
title: "DB_NAME (Transact-SQL)"
3-
description: "DB_NAME (Transact-SQL)"
3+
description: The DB_NAME function returns the name of a specified database.
44
author: VanMSFT
55
ms.author: vanto
6-
ms.date: "07/30/2017"
6+
ms.reviewer: randolphwest
7+
ms.date: 04/04/2025
78
ms.service: sql
89
ms.subservice: t-sql
910
ms.topic: reference
@@ -18,76 +19,88 @@ helpviewer_keywords:
1819
- "DB_NAME function"
1920
dev_langs:
2021
- "TSQL"
21-
monikerRange: ">= aps-pdw-2016 || = azuresqldb-current || = azure-sqldw-latest || >= sql-server-2016 || >= sql-server-linux-2017 || = azuresqldb-mi-current"
22+
monikerRange: ">=aps-pdw-2016 || =azuresqldb-current || =azure-sqldw-latest || >=sql-server-2016 || >=sql-server-linux-2017 || =azuresqldb-mi-current"
2223
---
2324
# DB_NAME (Transact-SQL)
25+
2426
[!INCLUDE [sql-asdb-asdbmi-asa-pdw](../../includes/applies-to-version/sql-asdb-asdbmi-asa-pdw.md)]
2527

2628
This function returns the name of a specified database.
27-
29+
2830
:::image type="icon" source="../../includes/media/topic-link-icon.svg" border="false"::: [Transact-SQL syntax conventions](../../t-sql/language-elements/transact-sql-syntax-conventions-transact-sql.md)
29-
30-
## Syntax
31-
31+
32+
## Syntax
33+
3234
```syntaxsql
33-
DB_NAME ( [ database_id ] )
34-
```
35-
35+
DB_NAME ( [ database_id ] )
36+
```
37+
3638
## Arguments
37-
*database_id*
3839

39-
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.
40-
40+
#### *database_id*
41+
42+
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.
43+
4144
## Return types
45+
4246
**nvarchar(128)**
43-
44-
## Permissions
4547

46-
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**.
47-
48+
## Permissions
49+
50+
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.
51+
52+
For the `master` database, `DB_ID` needs `CREATE DATABASE` permission at minimum.
53+
54+
The database to which the caller connects always appears in `sys.databases`.
55+
4856
> [!IMPORTANT]
49-
> 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.
50-
51-
## Examples
52-
53-
### A. Returning the current database name
57+
> 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.
58+
59+
## Examples
60+
61+
### A. Return the current database name
62+
5463
This example returns the name of the current database.
55-
64+
5665
```sql
57-
SELECT DB_NAME() AS [Current Database];
58-
GO
59-
```
60-
61-
### B. Returning the database name of a specified database ID
66+
SELECT DB_NAME() AS [Current Database];
67+
GO
68+
```
69+
70+
### B. Return the database name of a specified database ID
71+
6272
This example returns the database name for database ID `3`.
63-
73+
6474
```sql
65-
USE master;
66-
GO
67-
SELECT DB_NAME(3) AS [Database Name];
68-
GO
69-
```
70-
71-
## Examples: [!INCLUDE[ssazuresynapse-md](../../includes/ssazuresynapse-md.md)] and [!INCLUDE[ssPDW](../../includes/sspdw-md.md)]
72-
73-
### C. Return the current database name
74-
75+
USE master;
76+
GO
77+
78+
SELECT DB_NAME(3) AS [Database Name];
79+
GO
80+
```
81+
82+
## Examples: [!INCLUDE [ssazuresynapse-md](../../includes/ssazuresynapse-md.md)] and [!INCLUDE [ssPDW](../../includes/sspdw-md.md)]
83+
84+
### C. Return the current database name
85+
86+
This example returns the current database name.
87+
7588
```sql
76-
SELECT DB_NAME() AS [Current Database];
77-
```
78-
79-
### D. Return the name of a database by using the database ID
80-
This example returns the database name and database_id for each database.
81-
89+
SELECT DB_NAME() AS [Current Database];
90+
```
91+
92+
### D. Return the name of a database by using the database ID
93+
94+
This example returns the database name and `database_id` for each database.
95+
8296
```sql
83-
SELECT DB_NAME(database_id) AS [Database], database_id
84-
FROM sys.databases;
85-
```
86-
87-
## See also
88-
[DB_ID (Transact-SQL)](../../t-sql/functions/db-id-transact-sql.md)
89-
[Metadata Functions (Transact-SQL)](../../t-sql/functions/metadata-functions-transact-sql.md)
90-
[sys.databases (Transact-SQL)](../../relational-databases/system-catalog-views/sys-databases-transact-sql.md)
91-
92-
97+
SELECT DB_NAME(database_id) AS [Database],
98+
database_id
99+
FROM sys.databases;
100+
```
101+
102+
## Related content
93103

104+
- [DB_ID (Transact-SQL)](db-id-transact-sql.md)
105+
- [Metadata Functions (Transact-SQL)](metadata-functions-transact-sql.md)
106+
- [sys.databases (Transact-SQL)](../../relational-databases/system-catalog-views/sys-databases-transact-sql.md)

0 commit comments

Comments
 (0)