SQL SERVER – 2005 – Get Current User – Get Logged In User

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.

SQL SERVER - 2005 - Get Current User - Get Logged In User

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_USER and SUSER_SNAME() return the login name at the server level, like a Windows account or a SQL login.
  • USER_NAME() and CURRENT_USER return the database user, which is often dbo for administrators.
  • ORIGINAL_LOGIN() returns the login that first connected, even after EXECUTE AS switched the context. That makes it a good choice for auditing.
  • HOST_NAME() and APP_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.

SQL Function, SQL Scripts, SQL Server Security, SQL System Table
Previous Post
SQL SERVER – Deterministic Functions and Nondeterministic Functions
Next Post
SQL SERVER – 2008 – Server Consolidation WhitePaper Download

Related Posts

67 Comments. Leave new

  • if a quey returns a value then where is the security

    Reply
  • Dheeresh Verma
    June 25, 2012 5:27 pm

    @merlin you can use SP_WHO
    GO

    Reply
  • how to use login information to execute a sql thru task scheduler

    Reply
  • 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?

    Reply
  • 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

    Reply
  • What is the difference between sp_who and SELECT SYSTEM_USER

    Reply
  • can anyone help me please to show users name in side of my chatbox who are logging at that particular moment

    Reply
  • I can do this in the sql server studio but cannto get the current user in the view

    Reply
  • 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.

    Reply
  • Ever-Victoria
    May 14, 2015 8:44 am

    Hi
    How do i write a code to display logged in users information in a report using apex?

    Please help.

    Reply
  • thx Pinal!.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.