How to get current instance name from T-SQL
Asked Answered
L

9

119

How can I get the SQL Server server and instance name of the current connection, using a T-SQL script?

Lowis answered 6/8, 2013 at 0:57 Comment(1)
The accepted answer is correct. SELECT @@SERVERNAME produces the result needed to connect using sqlcmd -S. If it is the default MSSQLSERVER instance, then it must -not- be specified in the -S parameter. This is on 2017 14.0.2002.14 Developer edition, 64-bit.Selfinduced
L
208

Just found the answer, in this SO question (literally, inside the question, not any answer):

SELECT @@servername

returns servername\instance as far as this is not the default instance

SELECT @@servicename

returns instance name, even if this is the default (MSSQLSERVER)

Lowis answered 6/8, 2013 at 1:11 Comment(3)
@blasto, that seems to be the behavior when the instance is the default at the server. Try with a named instance. Check this: technet.microsoft.com/en-us/library/ms187944.aspxSubsidize
Incorrect, use @@servername can give you a wrong answer. SELECT CONVERT(sysname, SERVERPROPERTY('servername')) is the correct answer. The @@ServerName reports the SQL cluster name, while serverproperty('servername') reports the Windows cluster name. You need the Windows cluster name to connect to your db (The Windows cluster name can be different from the SQL cluster name; This usually happens when you install a new sql-server version on a different machine (@@servername = Environment.MachineName) and want to keep the old name so you don't have to change all configs).Columbine
@StefanSteiger The linked SO question's accepted answer indicates the solution you presented, but the comments indicate that it does not work for some people.Click
U
20

How about this:

EXECUTE xp_regread @rootkey='HKEY_LOCAL_MACHINE',
                   @key='SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQl',
                   @value_name='MSSQLSERVER'

This will get the instance name as well. null means default instance:

SELECT SERVERPROPERTY ('InstanceName')

http://technet.microsoft.com/en-us/library/ms174396.aspx

Unguiculate answered 6/8, 2013 at 1:3 Comment(3)
SELECT SERVERPROPERTY ('InstanceName') gives me a NULL.Ofay
instead use SELECT CONVERT(sysname, SERVERPROPERTY('servername'));Columbine
@Steam: Probably because it is the default-instance.Columbine
A
13

Why stop at just the instance name? You can inventory your SQL Server environment with following:

SELECT  
    SERVERPROPERTY('ServerName') AS ServerName,  
    SERVERPROPERTY('MachineName') AS MachineName,
    CASE 
        WHEN  SERVERPROPERTY('InstanceName') IS NULL THEN ''
        ELSE SERVERPROPERTY('InstanceName')
    END AS InstanceName,
    '' as Port, --need to update to strip from Servername. Note: Assumes Registered Server is named with Port
    SUBSTRING ( (SELECT @@VERSION),1, CHARINDEX('-',(SELECT @@VERSION))-1 ) as ProductName,
    SERVERPROPERTY('ProductVersion') AS ProductVersion,  
    SERVERPROPERTY('ProductLevel') AS ProductLevel,
    SERVERPROPERTY('ProductMajorVersion') AS ProductMajorVersion,
    SERVERPROPERTY('ProductMinorVersion') AS ProductMinorVersion,
    SERVERPROPERTY('ProductBuild') AS ProductBuild,
    SERVERPROPERTY('Edition') AS Edition,
    CASE SERVERPROPERTY('EngineEdition')
        WHEN 1 THEN 'PERSONAL'
        WHEN 2 THEN 'STANDARD'
        WHEN 3 THEN 'ENTERPRISE'
        WHEN 4 THEN 'EXPRESS'
        WHEN 5 THEN 'SQL DATABASE'
        WHEN 6 THEN 'SQL DATAWAREHOUSE'
    END AS EngineEdition,  
    CASE SERVERPROPERTY('IsHadrEnabled')
        WHEN 0 THEN 'The Always On Availability Groups feature is disabled'
        WHEN 1 THEN 'The Always On Availability Groups feature is enabled'
        ELSE 'Not applicable'
    END AS HadrEnabled,
    CASE SERVERPROPERTY('HadrManagerStatus')
        WHEN 0 THEN 'Not started, pending communication'
        WHEN 1 THEN 'Started and running'
        WHEN 2 THEN 'Not started and failed'
        ELSE 'Not applicable'
    END AS HadrManagerStatus,
    CASE SERVERPROPERTY('IsSingleUser') WHEN 0 THEN 'No' ELSE 'Yes' END AS InSingleUserMode,
    CASE SERVERPROPERTY('IsClustered')
        WHEN 1 THEN 'Clustered'
        WHEN 0 THEN 'Not Clustered'
        ELSE 'Not applicable'
    END AS IsClustered,
    '' as ServerEnvironment,
    '' as ServerStatus,
    '' as Comments
Aldebaran answered 25/5, 2017 at 21:12 Comment(1)
Good Stuff Nate, I have a similar more enhanced Inventory script doing the same. contact me for a copy by email... Script would be out of focus for this thread.Mothering
L
12

SELECT @@servername will give you data as server/instanceName

To get only the instanceName you should run select @@ServiceName query .

Legerdemain answered 18/5, 2015 at 10:49 Comment(1)
First, you have the slash the wrong way. The correct form is "server\instance". Second, it is not always true that @@servername will give you both the server AND the instance because when there is only a single instance it will return just "server".Pons
D
10

I found this:

EXECUTE xp_regread
        @rootkey = 'HKEY_LOCAL_MACHINE',
        @key = 'SOFTWARE\Microsoft\Microsoft SQL Server',
        @value_name = 'InstalledInstances'

That will give you list of all instances installed in your server.


The ServerName property of the SERVERPROPERTY function and @@SERVERNAME return similar information. The ServerName property provides the Windows server and instance name that together make up the unique server instance. @@SERVERNAME provides the currently configured local server name.

And Microsoft example for current server is:

SELECT CONVERT(sysname, SERVERPROPERTY('servername'));

This scenario is useful when there are multiple instances of SQL Server installed on a Windows server, and the client must open another connection to the same instance used by the current connection.

Divestiture answered 7/6, 2015 at 4:39 Comment(1)
SELECT CONVERT(sysname, SERVERPROPERTY('servername')); is the correct answerColumbine
A
4

To get the list of server and instance that you're connected to:

select * from Sys.Servers

To get the list of databases that connected server has:

SELECT * from sys.databases;
Acatalectic answered 28/2, 2020 at 21:5 Comment(0)
E
3

You can get your server name, machine name and instance name with Transact-SQL(T-SQL) as shown below:

SELECT @@SERVERNAME                    -- DESKTOP-OVPADTC\SQLEXPRESS
SELECT SERVERPROPERTY ('ServerName')   -- DESKTOP-OVPADTC\SQLEXPRESS

SELECT HOST_NAME()                     -- DESKTOP-OVPADTC
SELECT SERVERPROPERTY ('MachineName')  -- DESKTOP-OVPADTC

SELECT @@SERVICENAME                   -- SQLEXPRESS
SELECT SERVERPROPERTY ('InstanceName') -- SQLEXPRESS
Elenor answered 23/8, 2022 at 18:41 Comment(0)
A
2

Just to add some clarification to the registry queries. They only list the instances of the matching bitness (32 or 64) for the current instance.

The actual registry key for 32-bit SQL instances on a 64-bit OS is:

HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Microsoft SQL Server

You can query this on a 64-bit instance to get all 32-bit instances as well. The 32-bit instance seems restricted to the Wow6432Node so cannot read the 64-bit registry tree.

Amylum answered 6/8, 2019 at 3:47 Comment(0)
S
1

another method to find Instance name- Right clck on Database name and select Properties, in this part you can see view connection properties in left down corner, click that then you can see the Instance name.

Shope answered 5/7, 2018 at 9:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.