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

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

Discuss this Article 1

esaade
on Dec 14, 2010
is there an enhanced version for windows 2008?

Please or Register to post comments.

IT/Dev Connections

Las Vegas
September 30th - October 4th

Paul ThurottOur Experts will show you:
• Common SQL Server
Problems
• Best Practices for T-SQL
• SQL Server Integration
Services
• Database Development

Come See Mike Otey & Tim Ford in Person!

Early Registration Now Open

From the Blogs
May 21, 2013
blog

A Common Misconception about MAXDOP

Out of the box, SQL Server is (and has been) able to take advantage of multiple processors/cores without any effort on behalf of administrators....More
May 9, 2013
blog

My ISO 8601-Compliant Signature 2

My family recently just "officially" announced that we're in the process of adopting a child from South Africa. We're quite excited, of course, but there's a ton of paperwork to do—along with the need for gobs of signatures....More
May 8, 2013
blog

Use SSIS for ETL from Hadoop

In this blog post, Mark Kromer walks you through using SSIS as a way to use ETL techniques using Microsoft's Hadoop on Windows (HDInsight) as a source using Hive connectors...More
SQL Server Pro Forums

Get answers to questions, share tips, and engage with the SQL Server community in our Forums.