Posts Tagged ‘xp_regread’

SQL Server – Finding TCP Port Number SQL Instance is Listening on

01.05.2013 4 comments

By default SQL Server listens on TCP port number 1433, and for named instances TCP port is dynamically configured. There are several options available to get the listening port for SQL Server Instance.

Here are a few methods which we can use to get this information.

Method 1: SQL Server Configuration Manager

Method 2: Windows Event Viewer

Method 3: SQL Server Error Logs

Method 4: sys.dm_exec_connections DMV

Method 5: Reading registry using xp_instance_regread


Let's see how you can use each of these methods in detail:

Method 1: SQL Server Configuration Manager:

Step 1. Click Start > All Programs > Microsoft SQL Server 2012 > Configuration Tools > SQL Server Configuration Manager

Step 2. Go to SQL Server Configuration Manager > SQL Server Network Configuration > Protocols for <Instance Name>

Step 3. Right Click on TCP/IP and select Properties


Step 4. In TCP/IP Properties dialog box, go to IP Addresses tab and scroll down to IPAll group.


If SQL Server if configured to run on a static port it will be available in TCP Port textbox, and if it is configured on dynamic port then current port will be available in TCP Dynamic Ports textbox. Here my instance is listening on port number 61499.


Method 2: Windows Event Viewer:

When SQL Server is started it logs an event message as 'Server is listening on [ 'any' <ipv4> <port number>' in windows event logs. Here <port number> will be actual port number on which SQL Server is listening.

To view this using Event Viewer:

Step 1. Click Start > Administrative Tools > Event Viewer.

Note: If Administrative Tools are not available on Start menu, go to Start > Control Panel > System and Maintenance > Administrative Tools > View event logs

Step 2. Navigate to Event Viewer > Windows Logs > Application

Step 3. Since huge amount of event are logged, you need to use filtering to locate the required logs. Right click on Application and select Filter Current Log…


Step 4. You can filter the events by Event ID and Event source. The event we are interested in has Event ID of 26022, and it’s source is SQL Server Instance. You need to filter by both Event ID and SQL Server Instance if you have multiple instances installed, for a single instance you can filter by Event ID only. Click on OK to apply the filter.


Step 5. Once the filter is applied, Locate message 'Server is listening on [ 'any' <ipv4> …'. As we can see from below screenshot that SQL Server Instance is running on TCP Port 61499.



Method 3: SQL Server Error Logs:

When SQL Server is started it also logs an message to SQL Server Error Logs. You can search for port number in SQL Server Error Logs by opening SQL Server Error Log in notepad or via T-SQL using extended stored procedure xp_ReadErrorLog as below:

EXEC xp_ReadErrorLog 0, 1, N'Server is listening on', N'any', NULL, NULL, 'DESC'


Result Set:

LogDate                  ProcessInfo Text

2013-03-21 13:34:40.610  spid18s     Server is listening on [ ‘any’ <ipv4> 61499].

2013-03-21 13:34:40.610  spid18s     Server is listening on [ ‘any’ <ipv6> 61499].


(2 row(s) affected)

As we can see from the output that SQL Server Instance is listening on 61499.

Note: This method does not work if SQL Server Error Logs have been cycled. See sp_Cycle_ErrorLog for more information.


Method 4: sys.dm_exec_connections DMV:

DMVs return server state that can be used to monitor SQL Server Instance. We can use sys.dm_exec_connections DMV to identify the port number SQL Server Instance is listening on using below T-SQL code:

SELECT local_tcp_port

FROM   sys.dm_exec_connections

WHERE  session_id = @@SPID


Result Set:




(1 row(s) affected)

As we can see from the output… same as above Smile


Method 5: Reading registry using xp_instance_regread:

Port number can also be retrieved from Windows Registry database.

We can use extended stored procedure xp_instance_regread to get port number information using below T-SQL code:

DECLARE       @portNumber   NVARCHAR(10)


EXEC   xp_instance_regread

@rootkey    = 'HKEY_LOCAL_MACHINE',

@key        =

'SoftwareMicrosoftMicrosoft SQL ServerMSSQLServerSuperSocketNetLibTcpIpAll',

@value_name = 'TcpDynamicPorts',

@value      = @portNumber OUTPUT


SELECT [Port Number] = @portNumber


Result Set:

Port Number



(1 row(s) affected)

As we can see … same as above Smile Smile

Note: The above code will only work if SQL Server is configured to use dynamic port number. If SQL Server is configured on a static port, we need to use @value_name = 'TcpPort' as opposed to @value_name = 'TcpDynamicPorts'.

Hope This Helps!


Categories: Management Views and Functions, SQL Configuration, SQLServer, SQLServer 2005, SQLServer 2008, SQLServer 2008 R2, SQLServer 2012, Undocumented Functions Tags: 2008 R2, application event logs, check sql port, check sql server port no, configuration manager, Database, dmv, dm_exec_connextions, error logs, errorlos, event id, event id 26022, event viewer, find sql server port, finding the port number for a particular sql server instance, how to check the port number in sql server, how to determine whar sql server tcp ip port is being used, how to find port for sql, how to find sql runnig port, how to find sql server runnig port, how to find the port for ms sql server 2005, how to find the port for ms sql server 2008, how to find the port for ms sql server 2008 r2, how to find the port for ms sql server 2012, how to know sql server port number, identify sql server port, identify sql server port being used, identify tcp ip port, identify tcp ip port sql server, instance specific registry, ipall, key, local tcp port @@spid, local_tcp_port, logdate, Management Studio, processinfo, protocols for sql sevrer, Query, read registry tsql, rootkey, scripts, server is listening on, session_id, sp cycle error log, spid, sp_cycle_errorlog, SQL, Sql & Me, SQL 2005, sql 2005 port, SQL 2008, sql 2008 port, SQL 2008 R2, sql 2008 r2 port, sql 2012, sql 2012 port, Sql And Me, sql events, sql registry read reading registry using tsql reading registry using sql server, SQL Server, SQL Server 2005, sql server 2005 how to check port, SQL Server 2008, sql server 2008 how to check port, SQL Server 2008 R2, sql server 2008 r2 how to check port, sql server 2012, sql server 2012 how to check port, sql server change port, sql server configuration manager, sql server default port, sql server determine port, sql server error logs, sql server instance port, sql server listening port, sql server named instance port, sql server where can i find the tcp port configured, Sql&Me, SqlAndMe, SQLServer, SQLServer 2005, SQLServer 2008, SQLServer 2008 R2, SQLServer 2012, SSMS, supersocketnetlib, sys.dm_exec_connections, tcp, tcp dynamic ports, tcp port, tcp/ip port number, tcpip port, TSQL, value, value_name, Vishal Gajjar, what tcp port is sql server running under, windows logs application, xp instance regread, xp read error log, xp regread, xp_instance_regread, xp_readerrorlog, xp_regread

SQL Server – Identifying default data directory for multiple instances through registry

05.01.2012 9 comments

This is a follow up post from a comment on my blog.

Earlier I posted about using xp_instance_regread to get default data location for current instance. It translates a given registry path to instance specific registry path. This can return the value for a single instance only. However, if you need to locate default data directories for all available instances on a machine, you need to use xp_regread, which reads from an absolute registry path.

Registry path for default data location is same for different SQL Server versions:

HKLMSOFTWAREMicrosoftMicrosoft SQL Server<InstanceName>MSSQLServer


So the first thing we need before we can start reading data location from registry is list of instance name; these also can be read from registry using xp_instance_regenumvalues.

Registry path for instance name is as follows:

HKLMSOFTWAREMicrosoftMicrosoft SQL ServerInstance NamesSQL

We need to retrieve these to a temporary structure so that we can use it further:

CREATE TABLE #tempInstanceNames


      InstanceName      NVARCHAR(100),

      RegPath           NVARCHAR(100),

      DefaultDataPath   NVARCHAR(MAX)



INSERT INTO #tempInstanceNames (InstanceName, RegPath)

EXEC   master..xp_instance_regenumvalues

       @rootkey = N'HKEY_LOCAL_MACHINE',

       @key     = N'SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL'


SELECT      InstanceName, RegPath, DefaultDataPath

FROM        #tempInstanceNames

Result Set:

InstanceName  RegPath                    DefaultDataPath


DENALI3       MSSQL11.DENALI3            NULL

SQL08ENT      MSSQL10.SQL08ENT           NULL

SQL05EXP      MSSQL.1                    NULL

We have a list of instance names; now we need to iteratively read registry value for each path; and append it to DefaultDataPath. The paths we need to read from registry will be:


2. HKLMSOFTWAREMicrosoftMicrosoft SQL ServerMSSQL11.DENALI3MSSQLServer

3. HKLMSOFTWAREMicrosoftMicrosoft SQL ServerMSSQL10.SQL08ENTMSSQLServer

4. HKLMSOFTWAREMicrosoftMicrosoft SQL ServerMSSQL.1MSSQLServer

These needs to be read using xp_regread. I have used dynamic SQL to iterate through the list:


SET         @SQL = 'DECLARE @returnValue NVARCHAR(100)'


'EXEC   master.dbo.xp_regread

@rootkey      = N''HKEY_LOCAL_MACHINE'',

@key          = N''SOFTWAREMicrosoftMicrosoft SQL Server' + RegPath + 'MSSQLServer'',

@value_name   = N''DefaultData'',

@value        = @returnValue OUTPUT;


UPDATE #tempInstanceNames SET DefaultDataPath = @returnValue

WHERE RegPath = ''' + RegPath + '''' + CHAR(13) FROM #tempInstanceNames




SELECT      InstanceName, RegPath, DefaultDataPath

FROM        #tempInstanceNames

Result Set:

InstanceName         RegPath                    DefaultDataPath

MSSQLSERVER          MSSQL10_50.MSSQLSERVER     C:DatabaseData

DENALI3              MSSQL11.DENALI3            C:DatabaseDenaliData

SQL08ENT             MSSQL10.SQL08ENT           C:Database2008Data

SQL05EXP             MSSQL.1                    C:Database2005Data

I have updated the column in #tempInstanceNames so that it can be used further.

You can download the script from here.

Hope This Helps!