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:
- What is net_address here? Is is MAC Address or Some IP address etc?
- Is there any way to convert net_address to ip?
8条答案
按热度按时间wribegjk1#
it needs just single line of code
k10s72fa2#
Below query returns IP address and the workstation name of the client machine (not the server).
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.
nuypyhwy3#
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
... which can act in place of numeric IP address.
j9per5c44#
try this
6vl6ewon5#
In Windows Server 2019, utilize the following query to retrieve the clients IP Address information:
The result:
To get more details:
The result:
fbcarpbf6#
I found something which might work for you
From How to get Client IP Address in SQL Server
Also have a look at this article about Get client IP address
3b6akqbq7#
You can try out this solution . It even works on shared hosting:
jw5wzhpr8#
Ultimately join the two system tables:
Output: