Is it possible, and if so how, to get a user's remote IP address executing the query, analogously as we can get user's name with: SUSER_SNAME()
?
Update before bounty
I am looking for a solution which allows to grab the IP address of an ordinary mortal user, not a database owner. The ideas proposed by TheGameiswar or njc do not allow to capture a user's IP address who has been granted just a execute
permission. However, they are excellent ideas to start with the problem. Here I list the essence of the ideas:
Please see the sequence I follow:
create procedure MyStoredProcedure as
select client_net_address
from sys.dm_exec_connections
where session_id = @@SPID
Now add a user and grant permission:
CREATE LOGIN [user_mortal_jack] WITH PASSWORD=N'LongYouLive!!!';
GRANT EXECUTE ON MyStoredProcedure TO [user_mortal_jack];
When I run the procedure with a query:
EXECUTE AS USER = 'user_mortal_jack'
exec MyStoredProcedure
REVERT
I get error message:
The module being executed is not trusted. Either the owner of the database of the module needs to be granted authenticate permission, or the module needs to be digitally signed.
I will get this message even if I grant an additional permission:
grant VIEW SERVER STATE to [user_mortal_jack];
If I change the beginning of the stored procedure to:
create procedure MyStoredProcedure
with execute as OWNER as
I end up with different sort of error:
Could not obtain information about Windows NT group/user 'blahblah\admin_user', error code 0x534.
Update after bounty
Bounty is granted to Hadi for this single line of code hidden in their answer:
CONNECTIONPROPERTY('client_net_address')
which let's capture the IP address of any mortal user without neither granting any additional rights to the user nor setting the database TRUSTWORTHY ON option nor even creating a procedure WITH EXECUTE AS OWNER
clause.