• subscribe
October 31, 2005 12:00 AM

Dynamically Obtain a SQL Server’s IP Address in T-SQL Code

SQL Server Pro
InstantDoc ID #48303
Downloads
48303.zip

I created the getSQL_IPaddr.sql procedure to find the IP address of a server running Microsoft SQL Server in T-SQL code. As Listing 1 shows, the getSQL_IPaddr.sql procedure uses the xp_cmdshell extended stored procedure to run the Ipconfig utility.

The getSQL_IPaddr.sql procedure runs Ipconfig, which displays its results following the format

Windows IP Configuration
Ethernet adapter Local Area Connection:
Connection-specific DNS Suffix: xxxxx.xxxx.xxxx.xxx
IP Address: xxx.xxx.xxx.xxx
Subnet Mask: xxx.xxx.xxx.xxx
Default Gateway: xxx.xxx.xxx.xxx

Because I want only the IP address, the getSQL_IPaddr.sql procedure enters the Ipconfig output in a temporary table, then extracts the IP address.

To use the getSQL_IPaddr.sql procedure, you need permission to run xp_cmdshell from the master. Here’s an example of how to call the procedure:

declare @ip varchar(40)
exec sp_get_ip_address @ip out
print @ip 


ARTICLE TOOLS

Comments
  • Saade
    2 years ago
    Dec 14, 2010

    is there an enhanced version for windows 2008?

You must log on before posting a comment.

Are you a new visitor? Register Here