I know I can use SELECT SUSER_SNAME()
to get the current user name. But in an AD environment I would get the user including domain (e.g. "MyDomain\User").
Is there any function to just receive the username excluding domain?
The CURRENT_USER function returns the name of the current user in the SQL Server database.
SQL Server USER_NAME() Function The USER_NAME() function returns the database user name based on the specified id. If no id is specified, this function will return the name of the current user.
A Login that is mapped to the dbo User will show up as dbo for SESSION_USER when the "current" Database is the DB in question. AND, Logins in the sysadmin fixed server role (including Windows Logins that connect via a Windows Group that is member of the sysadmin fixed server role) will show up as dbo in SESSION_USER .
databases hosted by that server. You can see the user mappings by opening Sql Server Management Studio and connecting to your server. In the Object Explorer area expand the Security and then Login folders (just under "Databases"). Double-click a login to open it's Properties window, and find the User Mappings section.
select stuff(suser_sname(), 1, charindex('\', suser_sname()), '')
SELECT nt_username FROM sys.sysprocesses WHERE spid = @@SPID
or
SELECT nt_user_name FROM sys.dm_exec_sessions WHERE session_id = @@SPID
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With