Interesting enough Jr. DBA asked me how he can get current user for any particular query is ran. He said he wants it for debugging purpose as well for security purpose. I totally understand the need of this request. Knowing the current user can be extremely helpful in terms of security.

To get current user run following script in Query Editor
SELECT SYSTEM_USER
SYSTEM_USER will return current user. From Book On-Line – SYSTEM_USER returns the name of the currently executing context. If the EXECUTE AS statement has been used to switch context, SYSTEM_USER returns the name of the impersonated context.
Which Function to Use to Get Current User Details
SQL Server has several functions that sound alike but answer different questions. Here is how I keep them apart:
SYSTEM_USERandSUSER_SNAME()return the login name at the server level, like a Windows account or a SQL login.USER_NAME()andCURRENT_USERreturn the database user, which is often dbo for administrators.ORIGINAL_LOGIN()returns the login that first connected, even afterEXECUTE ASswitched the context. That makes it a good choice for auditing.HOST_NAME()andAPP_NAME()return the client computer and application names. They come from the client’s connection settings, so they are easy to fake and should not be used for security.
For a simple audit trail, add columns like CreatedBy with a default of SUSER_SNAME() and CreatedOn with a default of GETDATE(). Every insert then records who and when, without any change to the application.
If you need details for all sessions rather than just your own, sys.dm_exec_sessions lists login names, host names and program names for every connection, as long as you have VIEW SERVER STATE permission. @@SPID tells you which session is yours.
A quick test on a test server shows the difference. Run SELECT SUSER_SNAME(), USER_NAME(), ORIGINAL_LOGIN() as yourself, then run EXECUTE AS LOGIN = 'SomeLogin', run the same query again, and finish with REVERT. You will see SUSER_SNAME() switch to the new login while ORIGINAL_LOGIN() stays the same, which is exactly why auditors like it.
Published by Pinal Dave on SQLAuthority. More of my work at pinaldave.com.





67 Comments. Leave new
if a quey returns a value then where is the security
@merlin you can use SP_WHO
GO
how to use login information to execute a sql thru task scheduler
I am executing below query in sql server 2008 from code(C#) ,
SqlCommand dbCmd = new SqlCommand(“Execute as User=’newUser'”, connection);
connection.Open();
dbCmd.ExecuteNonQuery();
connection.Close();
it works fine.
and then I am creating temporary(starting with #) table,
CREATE TABLE #MyTable (Id INT, Name varchar(50))
but it throws exception “A severe error occurred on the current command. The results, if any, should be discarded.”
Here SqlConnection is in open state.
I have created “newUser” having reader, writer and reports permissions.
What should i do to run all queries under “newUser” from C# code?
Can anyone help me in creating a script to automatically map a login to a new database and make it a member of the db_owner
What is the difference between sp_who and SELECT SYSTEM_USER
can anyone help me please to show users name in side of my chatbox who are logging at that particular moment
I can do this in the sql server studio but cannto get the current user in the view
I have an sql 2K5 installation, there 2 vpn connections through dedicated lines connected to the sql, how can i see which user runs which query so i can kill the query if necessary? Thanx.
Hi
How do i write a code to display logged in users information in a report using apex?
Please help.
I can help you with query but don’t know how to use Apex.
Sp_who and sp_who2 system stored procedures will help you
thx Pinal!.