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.
Read more