How to get the client IP address from SQL Server 2008 itself?

后端 未结 6 1816
感动是毒
感动是毒 2020-12-05 02:11

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

相关标签:
6条回答
  • 2020-12-05 02:55

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

    select CONNECTIONPROPERTY('client_net_address') AS client_net_address 
    
    0 讨论(0)
  • 2020-12-05 02:58

    it needs just single line of code

     SELECT CONVERT(char(15), CONNECTIONPROPERTY('client_net_address'))
    
    0 讨论(0)
  • 2020-12-05 03:05

    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.

    0 讨论(0)
  • 2020-12-05 03:08

    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

    0 讨论(0)
  • 2020-12-05 03:10

    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
    
    0 讨论(0)
  • 2020-12-05 03:12

    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;
    
    0 讨论(0)
提交回复
热议问题