SQL - Query to get server's IP address

前端 未结 10 1400
情深已故
情深已故 2020-11-27 11:35

Is there a query in SQL Server 2005 I can use to get the server\'s IP or name?

相关标签:
10条回答
  • 2020-11-27 11:44

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

    SELECT @@SERVERNAME;
    

    To get only the hostname when 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 of the SQL script.

    0 讨论(0)
  • 2020-11-27 11:45
    select @@servername
    
    0 讨论(0)
  • 2020-11-27 11:48

    A simpler way to get the machine name without the \InstanceName is:

    SELECT SERVERPROPERTY('MachineName')
    
    0 讨论(0)
  • 2020-11-27 11:53

    Most solutions for getting the IP address via t-sql fall into these two camps:

    1. Run ipconfig.exe via xp_cmdshell and parse the output

    2. Query DMV sys.dm_exec_connections

    I'm not a fan of option #1. Enabling xp_cmdshell has security drawbacks, and there's lots of parsing involved anyway. That's cumbersome. Option #2 is elegant. And it's a pure 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, neither of the above queries works, though. Query #1 returns NULL if you're connected over Shared Memory (logged in and running SSMS on the SQL host). Query #2 may return nothing if there are no connections using a non-Shared Memory protocol. This scenario is likely when connected to a newly installed SQL instance. The solution? Force a connection over TCP/IP. To do this, create a new connection in SSMS and use the "tcp:" prefix with the server name. Then re-run either query and you'll get the IP address.

    0 讨论(0)
  • 2020-11-27 11:55

    The server might have multiple IP addresses that it is listening on. If your connection has the VIEW SERVER STATE server permission granted to it, you can run this query to get the address you have 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 shell out to the OS via xp_cmdshell, which is a technique that should be disabled (or at least strictly secured) on a production server. It may require you to grant VIEW SERVER STATE to the appropriate login, but that is a far smaller security risk than running xp_cmdshell.

    The technique mentioned by GilM for the server name is the preferred one:

    SELECT SERVERPROPERTY(N'MachineName');
    
    0 讨论(0)
  • 2020-11-27 11:55

    you can use command line query and execute in mssql:

    exec xp_cmdshell 'ipconfig'
    
    0 讨论(0)
提交回复
热议问题