40

I have a trigger for insert/update/delete. That is working fine. Also, I need the client's IP address from where the changes are made. That I need in T-SQL, that means, not in any web form but in the SQL/T-SQL while my trigger will get fired.

Also I go-ogled, and got that there is stored procedure in master database named xp_cmdshell which when executed with ipconfig we can get the IP Address. I think this will work only when you have administrative access to database. Mine hosting is a shared hosting , so I don't have such privilege. Is there any other way out?

Please note: I don't have administrative privileges on my SQL Server 2008 database. I need a solution as an authenticated user.

Another update:

I have got the solution, the query that will work for my scenario is

SELECT hostname, net_library, net_address
FROM sys.sysprocesses 
WHERE spid = @@SPID

It executes as needed but there is only one issue, that net_address is not in IP format. below is mine result:

hostname    net_library     net_address
IPC03       TCP/IP          AE8F925461DE  

I am eager to know:

  1. What is net_address here? Is is MAC Address or Some IP address etc?

  2. Is there any way to convert net_address to ip?

E_net4
  • 27,810
  • 13
  • 101
  • 139
  • 1
    You mention that your database is hosted, but how does the application work? In most ASP.NET applications, users connect to a website and the website then connects to the database. So the database server has no idea who or where the 'real' end user is. If you can give some more details about your application and how users connect to the database, someone may have a suggestion. – Pondlife Mar 30 '12 at 10:35
  • You can find the details about **net_address** from http://msdn.microsoft.com/en-us/library/ms179881.aspx – huMpty duMpty Mar 30 '12 at 14:40

7 Answers7

52

I found something which might work for you

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

From How to get Client IP Address in SQL Server

Also have a look at this article about Get client IP address

huMpty duMpty
  • 14,346
  • 14
  • 60
  • 99
  • 1
    found the same but you won with 20 sec :) – Peter Aron Zentai Mar 30 '12 at 10:23
  • 2
    Hi , seems query is fine but I got an error message - The user does not have permission to perform this action. Same scenario came here.. I dont have admin privilege –  Mar 30 '12 at 10:28
  • @AmitRanjan: Is it that you don't have permission to create the function or do the select from sys.dm_exec_connections? – Ben Thul Mar 30 '12 at 16:11
  • 2
    @AmitRanjan Selecting from `sys.dm_exec_connections` requires `VIEW SERVER STATE` permission http://msdn.microsoft.com/en-us/library/ms181509.aspx – Bryan Mar 30 '12 at 16:47
  • 2
    As @AmitRanjan pointed out this method requires permission to access sys.dm_exec_connections which may user don't have. Use select CONNECTIONPROPERTY('client_net_address') recommened by Dmitry below – Kenneth Xu Apr 02 '16 at 02:47
39

You can try out this solution. It even works on shared hosting:

select CONNECTIONPROPERTY('client_net_address') AS client_net_address 
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
Dmitry
  • 421
  • 4
  • 2
  • 3
    Link indicates that you can use `CONNECTIONPROPERTY('local_net_address') AS local_net_address` as well as a number of other CONNECTIONPROPERTY variables. – Trisped Nov 06 '14 at 01:37
  • 3
    This is a better solution as it doesn't need the permission to read sys.dm_exec_connections – Kenneth Xu Apr 02 '16 at 02:44
  • This shows your local address in case of SSMS. but in case of firing query by SPID, who is the SPID running client? this is not appropriate. – MarmiK Oct 27 '22 at 08:06
11

Ultimately join the two system tables:

SELECT  hostname,
        net_library,
        net_address,
        client_net_address
FROM    sys.sysprocesses AS S
INNER JOIN    sys.dm_exec_connections AS decc ON S.spid = decc.session_id
WHERE   spid = @@SPID

Output:

hostname | net_library | net_address | client_net_address    
PIERRE   | TCP/IP      | 0090F5E5DEFF| 10.38.168.5
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Pierre
  • 8,397
  • 4
  • 64
  • 80
6

it needs just single line of code

 SELECT CONVERT(char(15), CONNECTIONPROPERTY('client_net_address'))
Sukhdevsinh Zala
  • 1,126
  • 2
  • 14
  • 19
  • This may not work in case you are working in production. client address is just your machine address, instead the executer client's address. Pierre's answer above is more appropriate. – MarmiK Oct 31 '22 at 12:35
4

Below query returns IP address and the workstation name of the client machine (not the server).

SELECT CONNECTIONPROPERTY('client_net_address') AS IpAddress,
       HOST_NAME() AS ClientMachineName

Reference: https://learn.microsoft.com/en-us/sql/t-sql/functions/host-name-transact-sql?view=sql-server-ver15

Example

The following example creates a table that uses HOST_NAME() in a DEFAULT definition to record the workstation name of computers that insert rows into orders table.

CREATE TABLE Orders_demo  
(
  OrderID     INT        PRIMARY KEY,  
  OrderDate   DATETIME   NOT NULL,
  Workstation NCHAR(30)  NOT NULL     DEFAULT HOST_NAME()
);  
Vignesh Raja
  • 560
  • 6
  • 10
1

I couldn't get the exact numeric IP address, instead I got a NULL value because of the limitation of the above statements. The limit is that you only get IP addresses if you're connected via TCP/IP. If you're local and using Shared Memory then those attributes don't exist. If you turn off Shared Memory (or any protocols except for TCP/IP) via Server Configuration Manager you will always get IP address for any connection.

You are best stuck with

SELECT SERVERPROPERTY(N'MachineName');

... which can act in place of numeric IP address.

Chagbert
  • 722
  • 7
  • 16
0

try this

DECLARE @IP_Address varchar(255);
SELECT @IP_Address = client_net_address
FROM sys.dm_exec_connections
WHERE Session_id = @@SPID;

select @IP_Address;
Daniel Puiu
  • 962
  • 6
  • 21
  • 29
Kovid Purohit
  • 258
  • 1
  • 4
  • 15