Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / SQL

Get Client IP Address in SQL Server

4.79/5 (17 votes)
23 Feb 2012CPOL 91.6K  
How to get the client IP address in SQL Server

Introduction

Execute the below function and call this function through a Stored Procedure. It will return the IP address of the client system.

SQL
CREATE FUNCTION [dbo].[GetCurrentIP] ()
RETURNS varchar(255)
AS
BEGIN
    DECLARE @IP_Address varchar(255);
 
    SELECT @IP_Address = client_net_address
    FROM sys.dm_exec_connections
    WHERE Session_id = @@SPID;
 
    Return @IP_Address;
END

or we can get the same result by using the below query:

SQL
SELECT CONVERT(char(15), CONNECTIONPROPERTY('client_net_address'))

Enjoy.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)