In SQL Server, you can use the ORIGINAL_DB_NAME()
function to return the database name specified by the user in the database connection string.
This function is not to be confused with the DB_NAME()
function, which returns either a specific database, or the current one.
When you first make a connection to SQL Server, you have the option of specifying the initial database. For example, when using a command line interface such as sqlcmd, you can use the -d
parameter to specify the initial database. If you don’t use this parameter, your login’s default database will be the initial database.
Once you connect, you can switch to a different database, but your original database will always be the same. In other words, ORIGINAL_DB_NAME()
will always return the same database throughout your session, even if you switch to a different database.
Example 1 – Basic Usage
Here’s a basic example.
SELECT ORIGINAL_DB_NAME() AS [Original Database];
Result:
+---------------------+ | Original Database | |---------------------| | master | +---------------------+
Example 2 – Different Connection Strings
This example demonstrates how the output of the ORIGINAL_DB_NAME()
function is determined by the connection string.
Consider the following connection string:
mssql-cli -S localhost -U sa -P bigStrongPassword!!! -d Music
Here, I use the mssql-cli command line tool to connect to SQL Server. The part we’re interested in is -d Music
. This specifies that the initial database should be the Music database. In other words, once I’ve connected to SQL Server, my current database will be Music.
So after running that code (and successfully connecting to SQL Server), I can run ORIGINAL_DB_NAME()
:
SELECT ORIGINAL_DB_NAME() AS [Original Database];
Result:
+---------------------+ | Original Database | |---------------------| | Music | +---------------------+
Now, let’s open another connection, but this time I’ll specify a different database:
mssql-cli -S localhost -U sa -P bigStrongPassword!!! -d WideWorldImporters
Now run ORIGINAL_DB_NAME()
:
SELECT ORIGINAL_DB_NAME() AS [Original Database];
Result:
+---------------------+ | Original Database | |---------------------| | WideWorldImporters | +---------------------+
Example 3 – Switch Databases
Here’s what happens if I switch to a different database and run the statement again:
USE Music; SELECT ORIGINAL_DB_NAME() AS [Original Database];
Result:
Changed database context to 'Music'. +---------------------+ | Original Database | |---------------------| | WideWorldImporters | +---------------------+ (1 row affected)
The original database remains the same.
Here it is again when compared with the DB_NAME()
function:
USE Music; SELECT DB_NAME() AS [Current Database], ORIGINAL_DB_NAME() AS [Original Database];
Result:
Changed database context to 'Music'. +--------------------+---------------------+ | Current Database | Original Database | |--------------------+---------------------| | Music | WideWorldImporters | +--------------------+---------------------+ (1 row affected)
Example 4 – Default Database
Here’s what happens if I don’t explicitly specify a database in the connection string:
mssql-cli -S localhost -U sa -P bigStrongPassword!!!
Now run ORIGINAL_DB_NAME()
:
SELECT ORIGINAL_DB_NAME() AS [Original Database];
Result:
+---------------------+ | Original Database | |---------------------| | master | +---------------------+
In this case, the default database for that user is used.