SQL - Query to get server's IP address

前端 未结 10 1447
情深已故
情深已故 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    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');
    

提交回复
热议问题