Click here to Skip to main content
15,867,308 members
Articles / Programming Languages / SQL
Tip/Trick

Get Client IP Address in SQL Server

Rate me:
Please Sign up or sign in to vote.
4.79/5 (17 votes)
23 Feb 2012CPOL 88.6K   12   8
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)


Written By
Software Developer (Senior) Edge Business Solution
India India
Working in Edge Business Solution as a Senior Software Developer. I have more than 7 years exp. in Microsoft Technologies. I have exp. in ASP.Net, C#, SQL 2000/2005/2008 R2, Javascript, Ajax, JQuery, MVC, LINQ etc.

Comments and Discussions

 
QuestionSQL 2014? Pin
harveyt21-May-15 9:35
harveyt21-May-15 9:35 
Suggestionshort way Pin
Member 148265318-Jun-14 3:08
Member 148265318-Jun-14 3:08 
GeneralRe: short way Pin
Sarvesh Kumar Gupta24-Jun-14 21:15
Sarvesh Kumar Gupta24-Jun-14 21:15 
GeneralReason for my vote of 5 simple Pin
member601-Mar-12 0:21
member601-Mar-12 0:21 
Generalsimple Pin
member601-Mar-12 0:20
member601-Mar-12 0:20 
GeneralGood one. But when i login to my SSMS with Server as '.'. it... Pin
BhuvanRamIconcept27-Feb-12 18:15
BhuvanRamIconcept27-Feb-12 18:15 
GeneralReason for my vote of 5 gratz... Pin
Polinia27-Feb-12 5:39
Polinia27-Feb-12 5:39 
GeneralReason for my vote of 5 good one Pin
Nikhil_S24-Feb-12 18:29
professionalNikhil_S24-Feb-12 18:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.