SQL Server 2000
Programs |
MS SQL Server |
Client Network Utility |
Select TCP_IP then Properties
SQL Server 2005
Programs |
SQL Server |
SQL Server Configuration Manager |
Select Protocols for MSSQLSERVER or select Client Protocols and right click on TCP/IP
If you have run "netstat -a -b -n" (from an elevated command prompt) and you don't see "sqlservr.exe" at all then either your SQL Server service is not running or its TCP/IP network library is disabled.
Run SQL Server Configuration Manager (Start | All Programs | Microsoft SQL Server 2008 | Configuration Tools).
Navigate to SQL Server Services.
In the right-hand pane look for SQL Server (). Is it stopped? If so, start it.
Navigate to SQL Server Network Configuration (or SQL Server Network Configuration (32-bit) as appropriate) then Protocols for .
In the right-hand pane look for "TCP/IP". Is it disabled? If so, enable it, then restart the SQL Server service.
Note that he Instance ID will be MSSQLSERVER for the default instance.
Please also note that you don't have to enable the TCP/IP network library to connect a client to the service. Clients can also connect through the Shared Memory network library (if the client is on the same machine) or the Named Pipes network library.
-- Find Database Port script by Jim Pierce 09/05/2018
USE [master]
GO
DECLARE @DynamicportNo NVARCHAR(10);
DECLARE @StaticportNo NVARCHAR(10);
DECLARE @ConnectionportNo INT;
-- Look at the port for the current connection
SELECT @ConnectionportNo = [local_tcp_port]
FROM sys.dm_exec_connections
WHERE session_id = @@spid;
-- Look for the port being used in the server's registry
EXEC xp_instance_regread @rootkey = 'HKEY_LOCAL_MACHINE'
,@key =
'Software\Microsoft\Microsoft SQL Server\MSSQLServer\SuperSocketNetLib\Tcp\IpAll'
,@value_name = 'TcpDynamicPorts'
,@value = @DynamicportNo OUTPUT
EXEC xp_instance_regread @rootkey = 'HKEY_LOCAL_MACHINE'
,@key =
'Software\Microsoft\Microsoft SQL Server\MSSQLServer\SuperSocketNetLib\Tcp\IpAll'
,@value_name = 'TcpPort'
,@value = @StaticportNo OUTPUT
SELECT [PortsUsedByThisConnection] = @ConnectionportNo
,[ServerStaticPortNumber] = @StaticportNo
,[ServerDynamicPortNumber] = @DynamicportNo
GO
Perhaps not the best options but just another way is to read the Windows Registry in the host machine, on elevated PowerShell prompt you can do something like this:
#Get SQL instance's Port number using Windows Registry:
$instName = (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server').InstalledInstances[0]
$tcpPort = (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\$instName\MSSQLServer\SuperSocketNetLib\Tcp").TcpPort
Write-Host The SQL Instance: `"$instName`" is listening on `"$tcpPort`" "TcpPort."
Ensure to run this PowerShell script in the Host Server (that hosts your SQL instance / SQL Server installation), which means you have to first RDP into the SQL Server/Box/VM, then run this code.
If you don't want to look in SQL Server Management (sqlservermanager15.msc), then run this query in the database, e.g. from sqlcmd or ssms:
SELECT * FROM [sys].[dm_tcp_listener_states]
listener_id
ip_address
is_ipv4
port
type
type_desc
state
state_desc
start_time
1
::1
False
1433
0
TSQL
0
ONLINE
2021-01-01 00:00:00.000000
2
127.0.0.1
True
1433
0
TSQL
0
ONLINE
2021-01-01 00:00:00.000000
Thanks to @vladimir-bashutin for pointing out this one. Here is another one:
SELECT [name]
,[protocol_desc]
,[type_desc]
,[state]
,[state_desc]
,[is_admin_endpoint]
FROM [master].[sys].[endpoints]
name
protocol_desc
type_desc
state
state_desc
is_admin_endpoint
TSQL Local Machine
SHARED_MEMORY
TSQL
0
STARTED
False
TSQL Named Pipes
NAMED_PIPES
TSQL
0
STARTED
False
TSQL Default TCP
TCP
TSQL
0
STARTED
False
TSQL Default VIA
VIA
TSQL
0
STARTED
False
So now you have the port and protocol. If you don't have access to these system tables, consider using an SSRP client, such as https://github.com/adzm/ssrpc.