Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I query the name of the current SQL Server database instance?

It is a bit of a "chicken or egg" kind of query, but can someone dreamup a query that can return the name of the current database instance in which the query executes? Believe me when I say I understand the paradox: why do you need to know the name of the database instance if you're already connected to execute the query? Auditing in a multi-database environment.

I've looked at all the @@ globals in Books Online. "SELECT @@servername" comes close, but I want the name of the database instance rather than the server.

like image 900
flipdoubt Avatar asked Sep 24 '08 20:09

flipdoubt


People also ask

How do I find SQL Server instance details?

View instance details using SSMSUnder the General tab of the Server Properties window, some basic SQL Server Instance information is displayed: Product – the name of the product and its bit version. Operating system – information about the operating system the instance is installed on.

How do I find the current server name in SQL Server?

Open up SQL Server Configuration Manager (search for it in the Start menu). Click on SQL Server Services . The instance name of SQL Server is in parenthesis inline with SQL Server service. If it says MSSQLSERVER, then it's the default instance.

How do I find the database name on a server?

In Microsoft SQL Server Management Studio, in the Object Explorer pane, right click the server and select properties. In the pane, there should be a heading called "Connection" and in that heading a link to a new window called "View connection properties". The value next to "Server name" is the name of your server.


2 Answers

SELECT DB_NAME() 

Returns the database name.

like image 115
Dana Avatar answered Sep 18 '22 08:09

Dana


SELECT  @@servername AS 'Server Name' -- The database server's machine name ,@@servicename AS 'Instance Name' -- e.g.: MSSQLSERVER ,DB_NAME() AS 'Database Name' ,HOST_NAME() AS 'Host Name' -- The database client's machine name 
like image 22
kevin Avatar answered Sep 21 '22 08:09

kevin