Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server: Get current user without domain

Tags:

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?

like image 666
Dennis G Avatar asked Jul 06 '11 09:07

Dennis G


People also ask

How can check current user in SQL Server?

The CURRENT_USER function returns the name of the current user in the SQL Server database.

How can I see the username in SQL?

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.

Why current user is DBO?

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 .

How do I find my SQL Server username and password?

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.


2 Answers

select stuff(suser_sname(), 1, charindex('\', suser_sname()), '') 
like image 94
Mikael Eriksson Avatar answered Oct 11 '22 19:10

Mikael Eriksson


SELECT nt_username FROM sys.sysprocesses WHERE spid = @@SPID 

or

SELECT nt_user_name FROM sys.dm_exec_sessions WHERE session_id = @@SPID 
like image 40
gbn Avatar answered Oct 11 '22 17:10

gbn