How to find TCP\IP port via T-SQL

In most of the forums I’ve seen that users are not able to find on which port does their sql server is listening. Hence I decided to write a script to check TCP\IP port on which sql server is listening. Checkout the below script.

Script

DECLARE @key VARCHAR(50), @RegistryPath VARCHAR(200)
IF (SERVERPROPERTY('INSTANCENAME')) IS NULL
BEGIN
SET @RegistryPath='Software\Microsoft\MSSQLServer\MSSQLServer\SuperSocketNetLib\Tcp\'
END
ELSE
BEGIN
SET @RegistryPath='Software\Microsoft\Microsoft SQL Server\'+CONVERT(VARCHAR(25),SERVERPROPERTY('INSTANCENAME')) + '\MSSQLServer\SuperSocketNetLib\Tcp\'
END
EXEC master.dbo.xp_regread 'HKEY_LOCAL_MACHINE', @RegistryPath, 'tcpPort'

Output

Value Data
tcpPort 1433
VN:F [1.9.13_1145]
Rating: 5.0/5 (2 votes cast)
VN:F [1.9.13_1145]
Rating: +1 (from 1 vote)
How to find TCP\IP port via T-SQL, 5.0 out of 5 based on 2 ratings

Comments are closed.