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.

To get current user run following script in Query Editor


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.

Reference : Pinal Dave (http://blog.SQLAuthority.com), BOL

67 thoughts on “SQL SERVER – 2005 – Get Current User – Get Logged In User

  1. You can also try:


    I found that select current_user was returning ‘dbo’. As my company is using windows accounts to connect to a database server the above command returned the result I was after.


  2. In an interview, i was asked what query can you write to see what version of windows is SQL sever running on??? i couldn’t find the answer using google.??


  3. Hello

    I am trying to connect php and SQL server together. No matter how hard I try I always get the following error in apache error log
    undefined function mssql_connect().

    I am not able to connect at all, and always get HTTP 500 error. I have completed all procedures successfully like updating ntwdblib.dll, copying php_mssql.dll and php_sqlsrv.dll in the correct folders.

    I have also changed php.ini files to uncomment php_mssql.dll and php_sqlsrv.dll.

    I have XP service pack2, WAMP version 2.0, SQL server 2005 9.00.3042.00(Intel X86), PHP 5.2.9-1 and Apache 2.2.11.

    Any help be greatly apprecciated


  4. I want to know that Is it possible to track a user activities logged into sql server 2005.
    for example if i login using “sa” or else. then track the activity for sa user .


  5. I Searched and searched and finally found the RIGHT answer…


    Returns the name of the login that connected to the instance of SQL Server. You can use this function to return the identity of the original login in sessions in which there are many explicit or implicit context switches.


  6. ORIGINAL_LOGIN() returns the login name of the original context, but what will return the original username. I have the case where I need to know the username, not the login. Or can I take the login information and get the username associated with it with the sys tables?


  7. i need to write a script to find all SQL authenticated current users in an instance…. please any ine help me…


  8. I need to write a script to find only all SQL authenticated users (including both logged in and logged out ) who all are connecting to an instance in a server ….

    Please any one help me…


    • You could create an alert which is activated when a user logs in to SQL Server. I believe this is the SQLServer:GeneralStatistics performance object, and the counter ‘User Connections’. You could get the alert to execute a stored proc to log the user. leave it to run for a period of time and then collect your results. I’m sure there will be an easier way though via a system table.

      You could use SQL Profiler to monitor if it’s not a huge user base?


  9. Hi all,
    I am trying to develop a script that capture the user id/username of the user who is using ms dynamics 9.0 gp when the user edit a record. Could u help please.
    Thank u


  10. CREATE FUNCTION getNTUser (@spid int)
    RETURNS varchar(50) AS
    DECLARE @ntUser varchar(50)
    select @ntUser = nt_username from master..sysprocesses where spid = @spid
    return @ntUser

    declare @UserName nvarchar(16)

    select @UserName = dbo.GetNTUser(@@SPID)


  11. hi pinal,

    provide me the query for find out all database login with user detail (when with which user has log in and when out) detail within particular server.


  12. Hi pinal,
    is it possible to track all activities (edit,insert,delete,enabling/disabling of triggers,editing of SP,etc) and create a log on SQL Server?
    Activities can be from any instance and can we track the machine name too of the client machine??


  13. Hi,

    I’m trying to write a sql query to graph data on Joomla’s plotalot extension. There are two users (user ID 1, and user ID 2). Bother users log into the front page and enter a score for that day (Nov 1, score: 3. Nov 2, score 9, Nov 3: score 4, etc….) Now I want a user to be able to login (let’s say user ID 1), and see only his scores. To built the plotalot chart, I use the query: SELECT UNIX_TIMESTAMP(input_datetime_3), input_select_2 FROM jos_chronoforms_data_datascores2
    WHERE cf_user_id = 1.

    But how do I write a query that will identify whoever is logged in? I don’t want to say: “WHERE cf_user_id = 1” Instead, I want to say: “WHERE cf_user_id = (currently logged in user).

    Any help is greatly appreciated!


  14. Hi… can i know what is the parameter for who is currently login to the computer in order to run the query… i need to know who is currently login to a computer… not to sql server…


  15. I need to build an sql of all users who have logged the last three months. I’ve trying to understand system sp_who, but I don understand how to set start date of results.


  16. I am executing below query in sql server 2008 from code(C#) ,

    SqlCommand dbCmd = new SqlCommand(“Execute as User=’newUser'”, connection);

    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?


  17. 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.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s