0

I'm wondering if there is a SQL query that would show the IP of the client that wrote the query. Like if I have people making changes to my database I already have a log table but I want to add IP address to it. So I am wondering if this this possible.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Psycnosis
  • 25
  • 1
  • 5
  • 2
    Not unless the IP address is stored in the database. Do you understand how SQL queries work? – Gordon Linoff Feb 28 '15 at 16:34
  • 2
    http://stackoverflow.com/questions/142142/sql-query-to-get-servers-ip-address , http://stackoverflow.com/questions/9941074/how-to-get-the-client-ip-address-from-sql-server-2008-itself – Mitch Wheat Feb 28 '15 at 16:36
  • If it's an interactive session then it probably makes sense to speak in terms of "the client that wrote the query." I think you mean "submitted the query" or "executed the query". Unfortunately too many people are ready to pounce on confusing language. – shawnt00 Feb 28 '15 at 18:22
  • figured this wasn't possible but had to ask. I am relatively new to SQL. – Psycnosis Feb 28 '15 at 18:28

1 Answers1

1

If the connection was made with TCP, the query below will return the client IP address. Note that VIEW SERVER STATE permissions are required with this method.

SELECT client_net_address
FROM sys.dm_exec_connections
WHERE 
    session_id = @@SPID
    AND net_transport = 'TCP';
Dan Guzman
  • 43,250
  • 3
  • 46
  • 71