SQL - query to get server IP address

Is there a query in SQL Server 2005 that I can use to get the IP address or server name?

+58
sql sql-server
Sep 26 '08 at 21:37
source share
10 answers
SELECT CONNECTIONPROPERTY('net_transport') AS net_transport, CONNECTIONPROPERTY('protocol_type') AS protocol_type, CONNECTIONPROPERTY('auth_scheme') AS auth_scheme, CONNECTIONPROPERTY('local_net_address') AS local_net_address, CONNECTIONPROPERTY('local_tcp_port') AS local_tcp_port, CONNECTIONPROPERTY('client_net_address') AS client_net_address 

The code here will give you an IP address;

This will work for remote client query on SQL 2008 and newer.

If you use Permissions for shared memory, then doing the above on the server itself will give you

  • Shared memory as the value for net_transport and
  • NULL for 'local_net_address' and
  • ' <local machine> ' will be displayed in 'client_net_address'.

"client_net_address" is the address of the computer to which the request was sent, while "local_net_address" will be the SQL server (thus NULL over shared memory connections) and the address you would give someone if they cannot use the server name NetBios or FQDN for any reason.

I highly recommend using this answer . Enabling the shell is a very bad idea on production SQL Server.

+83
Feb 04 '13 at 20:59
source share

You can get the name [hostname] \ [instancename]:

 SELECT @@SERVERNAME; 

To get only hostname if you have hostname \ instance name format:

 SELECT LEFT(ltrim(rtrim(@@ServerName)), Charindex('\', ltrim(rtrim(@@ServerName))) -1) 

Alternatively, as @GilM pointed out:

 SELECT SERVERPROPERTY('MachineName') 

You can get the actual IP address using this:

 create Procedure sp_get_ip_address (@ip varchar(40) out) as begin Declare @ipLine varchar(200) Declare @pos int set nocount on set @ip = NULL Create table #temp (ipLine varchar(200)) Insert #temp exec master..xp_cmdshell 'ipconfig' select @ipLine = ipLine from #temp where upper (ipLine) like '%IP ADDRESS%' if (isnull (@ipLine,'***') != '***') begin set @pos = CharIndex (':',@ipLine,1); set @ip = rtrim(ltrim(substring (@ipLine , @pos + 1 , len (@ipLine) - @pos))) end drop table #temp set nocount off end go declare @ip varchar(40) exec sp_get_ip_address @ip out print @ip 

Source SQL script .

+34
Sep 26 '08 at 21:41
source share

A server may have several IP addresses that it is listening on. If your connection has VIEW SERVER STATE view server permission, you can run this query to get the address that you connected to SQL Server:

 SELECT dec.local_net_address FROM sys.dm_exec_connections AS dec WHERE dec.session_id = @@SPID; 

This solution does not require you to unload into the OS via xp_cmdshell, which is a method that must be disabled (or at least strictly protected) on the production server. This may require that you grant VIEW SERVER view status to the appropriate login, but this is a much lower security risk than running xp_cmdshell.

Preferred technology mentioned by GilM for server name:

 SELECT SERVERPROPERTY(N'MachineName'); 
+15
Mar 08 '12 at 18:26
source share

It is in the variable @@ SERVERNAME ;

 SELECT @@SERVERNAME; 
+8
Sep 26 '08 at 21:38
source share

Most solutions for obtaining an IP address through t-sql fall into these two camps:

  • Run ipconfig.exe via xp_cmdshell and parse the output

  • DMV request sys.dm_exec_connections

I am not a fan of option number 1. The inclusion of xp_cmdshell has security flaws, in which case there is a lot of parsing. This is cumbersome. Option number 2 is elegant. And this is a clean t-sql solution, which I almost always prefer. Here are two sample queries for option # 2:

 SELECT c.local_net_address FROM sys.dm_exec_connections AS c WHERE c.session_id = @@SPID; SELECT TOP(1) c.local_net_address FROM sys.dm_exec_connections AS c WHERE c.local_net_address IS NOT NULL; 

Sometimes none of the above requests work. Query # 1 returns NULL if you are connected to shared memory (logging in and running SSMS on the SQL host). Request No. 2 may return nothing if there are no connections using the protocol without shared memory. This scenario is likely to be associated with a new instance of SQL. Decision? Configure the connection via TCP / IP. To do this, create a new connection in SSMS and use the prefix "tcp:" with the server name. Then re-run any request and you will get an IP address.

SSMS - Connect to the database engine

+6
Nov 11 '15 at 14:32
source share
 select @@servername 
+3
Sep 26 '08 at 21:38
source share

An easier way to get the name of a machine without the name \ InstanceName:

 SELECT SERVERPROPERTY('MachineName') 
+2
Sep 26 '08 at 23:49
source share

you can use a command prompt and execute in mssql:

 exec xp_cmdshell 'ipconfig' 
+2
Aug 04 '16 at 5:10
source share

- Try this script, it works for my needs. Reformat it.

SELECT
SERVERPROPERTY ('ComputerNamePhysicalNetBios') as 'Is_Current_Owner', SERVERPROPERTY ('MachineName') as 'MachineName', the case when @@ ServiceName = Right (@@ Servername, len (@@ ServiceName)) and then @@ Servername else @ @servername + '\' + @@ Servicename end as '@@ Servername \ Servicename',
CONNECTIONPROPERTY ('net_transport') AS net_transport, CONNECTIONPROPERTY ('local_tcp_port') AS local_tcp_port, dec.local_tcp_port, CONNECTIONPROPERTY ('local_net_address') AS local_net_address, dec.local_net_ocal_dec_sec_dnsec_dnsec_dnsec_dnsec_dnsec_dnsec_dnsec_dnsec_dnsec_dnsec_dnsec_dnsec_dnsec_dnsec_dnsec_dnsec_dnsec_dnsec_doc = @@ SPID;

+1
Nov 06 '17 at 19:11
source share

I know this is an old post, but perhaps this solution can be useful when you want to get the IP address and TCP port from a shared memory connection (for example, from a script in SSMS locally on the server). The key is to open an additional connection to your SQL Server using OPENROWSET, in which you specify "tcp:" in your connection string. The rest of the code simply creates dynamic SQL to bypass the OPENROWSET constraint, which is unable to accept variables as parameters.

 DECLARE @ip_address varchar(15) DECLARE @tcp_port int DECLARE @connectionstring nvarchar(max) DECLARE @parm_definition nvarchar(max) DECLARE @command nvarchar(max) SET @connectionstring = N'Server=tcp:' + @@SERVERNAME + ';Trusted_Connection=yes;' SET @parm_definition = N'@ip_address_OUT varchar(15) OUTPUT , @tcp_port_OUT int OUTPUT'; SET @command = N'SELECT @ip_address_OUT = a.local_net_address, @tcp_port_OUT = a.local_tcp_port FROM OPENROWSET(''SQLNCLI'' , ''' + @connectionstring + ''' , ''SELECT local_net_address , local_tcp_port FROM sys.dm_exec_connections WHERE session_id = @@spid '') as a' EXEC SP_executeSQL @command , @parm_definition , @ip_address_OUT = @ip_address OUTPUT , @tcp_port_OUT = @tcp_port OUTPUT; SELECT @ip_address, @tcp_port 
0
Feb 24 '16 at 14:04
source share



All Articles