The SQL Server instances are independent and do not know each other. The method is to query the registry to find the instances.
You can use the following Script to list all the installed sql server on the server using TSQL
Solution
=========
SetNoCountOn
Declare @CurrID int,@ExistValue int, @MaxID int, @SQL nvarchar(1000)
Declare @TCPPorts Table(PortType nvarchar(180), Port int)
Declare @SQLInstances Table(InstanceID intidentity(1, 1)notnullprimarykey,
InstName nvarchar(180),
Folder nvarchar(50),
StaticPort intnull,
DynamicPort intnull,
Platformintnull);
Declare @Plat Table(Id int,Namevarchar(180),InternalValue varchar(50), Charactervalue varchar(50))
Declare @Platform varchar(100)
Insertinto @Plat execxp_msverplatform
select @Platform =(select 1 from @plat where charactervalue like'%86%')
If @Platform isNULL
Begin
InsertInto @SQLInstances (InstName, Folder)
Exec xp_regenumvalues N'HKEY_LOCAL_MACHINE',
N'SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL';
Update @SQLInstances setPlatform=64
End
else
Begin
InsertInto @SQLInstances (InstName, Folder)
Exec xp_regenumvalues N'HKEY_LOCAL_MACHINE',
N'SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL';
Update @SQLInstances SetPlatform=32
End
Declare @Keyexist Table(Keyexist int)
Insertinto @Keyexist
Exec xp_regread'HKEY_LOCAL_MACHINE',
N'SOFTWARE\Wow6432Node\Microsoft\Microsoft SQL Server\Instance Names\SQL';
select @ExistValue= Keyexist from @Keyexist
If @ExistValue=1
InsertInto @SQLInstances (InstName, Folder)
Exec xp_regenumvalues N'HKEY_LOCAL_MACHINE',
N'SOFTWARE\Wow6432Node\Microsoft\Microsoft SQL Server\Instance Names\SQL';
Update @SQLInstances SetPlatform=32 wherePlatformisNULL
Select @MaxID =MAX(InstanceID), @CurrID = 1
From @SQLInstances
While @CurrID <= @MaxID
Begin
DeleteFrom @TCPPorts
Select @SQL ='Exec xp_instance_regread N''HKEY_LOCAL_MACHINE'',
N''SOFTWARE\Microsoft\\Microsoft SQL Server\'+ Folder +'\MSSQLServer\SuperSocketNetLib\Tcp\IPAll'',
N''TCPDynamicPorts'''
From @SQLInstances
Where InstanceID = @CurrID
InsertInto @TCPPorts
Execsp_executesql @SQL
Select @SQL ='Exec xp_instance_regread N''HKEY_LOCAL_MACHINE'',
N''SOFTWARE\Microsoft\\Microsoft SQL Server\'+ Folder +'\MSSQLServer\SuperSocketNetLib\Tcp\IPAll'',
N''TCPPort'''
From @SQLInstances
Where InstanceID = @CurrID
InsertInto @TCPPorts
Execsp_executesql @SQL
Select @SQL ='Exec xp_instance_regread N''HKEY_LOCAL_MACHINE'',
N''SOFTWARE\Wow6432Node\Microsoft\\Microsoft SQL Server\'+ Folder +'\MSSQLServer\SuperSocketNetLib\Tcp\IPAll'',
N''TCPDynamicPorts'''
From @SQLInstances
Where InstanceID = @CurrID
InsertInto @TCPPorts
Execsp_executesql @SQL
Select @SQL ='Exec xp_instance_regread N''HKEY_LOCAL_MACHINE'',
N''SOFTWARE\Wow6432Node\Microsoft\\Microsoft SQL Server\'+ Folder +'\MSSQLServer\SuperSocketNetLib\Tcp\IPAll'',
N''TCPPort'''
From @SQLInstances
Where InstanceID = @CurrID
InsertInto @TCPPorts
Execsp_executesql @SQL
Update SI
Set StaticPort = P.Port,
DynamicPort = DP.Port
From @SQLInstances SI
InnerJoin @TCPPorts DP On DP.PortType ='TCPDynamicPorts'
InnerJoin @TCPPorts P On P.PortType ='TCPPort'
Where InstanceID = @CurrID;
Set @CurrID = @CurrID + 1
End
Selectserverproperty('ComputerNamePhysicalNetBIOS')as ServerName, InstName, StaticPort, DynamicPort,Platform
From @SQLInstances
SetNoCountOff
Note: The above script may not return the desired results when run on 32 bit Edition of Sql Server which is installed on 64 bit Windows Server
Microsoft Sql Server