Q
Problem solve Get help with specific problems with your technologies, process and projects.

Query to return database name

Global function DB_NAME() allows you to return the name of a database that a process is connected to. SQL Server 2005 expert Adam Machanic offers a sample query in this response.

What query can I use to expose the name of the database a process connects to in SQL Server 2005 and previous versions? For example, within an Oracle database, the query would be "SELECT SYS.DATABASE_NAME FROM DUAL." This query would echo the name of the Oracle database connected to an SQR process.
You can use the DB_NAME() global function in either SQL Server 2000 or SQL Server 2005 to get that data. Use something like the following query:
SELECT DB_NAME() AS DatabaseName
This query will return the name of the database that the session is currently connected to. Note that in SQL Server, unlike Oracle, the "DUAL" table is unnecessary for selecting scalar values from functions or other non-table sources.


Do you have comments on this Ask the Expert Q&A? Let us know.

Dig Deeper on Microsoft SQL Server 2005

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.

-ADS BY GOOGLE

SearchBusinessAnalytics

SearchDataCenter

SearchDataManagement

SearchAWS

SearchOracle

SearchContentManagement

SearchWindowsServer

Close