You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
+
41
44
## Return types
45
+
42
46
**nvarchar(128)**
43
-
44
-
## Permissions
45
47
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
+
48
56
> [!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
+
54
63
This example returns the name of the current database.
55
-
64
+
56
65
```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
+
62
72
This example returns the database name for database ID `3`.
63
-
73
+
64
74
```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
+
75
88
```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
+
82
96
```sql
83
-
SELECT DB_NAME(database_id) AS [Database], database_id
0 commit comments