How to Get Windows Log-In User Name for a SQL Log in User

How to get Windows Log-in User Name for a SQL Log in User

There is no link between a SQL login and the NT username.

You asked similar here: How to find out user name and machine name to access to SQL server

The WMI approach will be ambiguous if more than 1 user is logged into the client PC (eg service accounts, remote user via mstsc etc). Any approach like this will require admin rights on the client PC too for the account used.

I doubt you can do it in real time.

All you can do is record the client_net_address in sys.dm_exec_connections and backtrack from there, perhaps via WMI but not from SQL Server itself.

Do you need the username though? Or just the client PC so you can change the app connection string?

You final solution is to change the sa password and see who calls, if you only have relatively few SQL connections

I want to get windows login name instead of SQL login name

Use SUSER_NAME() or SUSER_SNAME()

If neither of these are returning your AD account you logged in via, check you are using AD trusted connections and not SQL auth.

https://learn.microsoft.com/en-us/sql/t-sql/functions/suser-sname-transact-sql?view=sql-server-2017

Get login name in SQL SERVER

execute as login = 'LCF\jmp'
select distinct name
from sys.login_token
where principal_id > 0
and type = 'WINDOWS GROUP';

Change Login name (Windows user) for an existing SQL user on the Database server

When SQL Server user domain name changes, at this time, we still want to keep the current database user setting and don't want to recreate those users, we can use SQL script to update the user domain as follow:

ALTER USER [abcd] WITH LOGIN = [DomainB\abcd], NAME = [abcd]

I am glad that works for you.

Get Full name of User logged-in using login Details in C# windows form Application

You can use an outer or left join to represent everything in one table and only the matched items in the other.

select 
*
FROM
TABLEALL
LEFT JOIN
TABLESOME
ON
TABLEALL.ID = TABLESOME.ID


Related Topics



Leave a reply



Submit