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

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.

Reference : Pinal Dave (https://blog.sqlauthority.com), BOL

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

  • Chris Marshall
    July 16, 2008 2:54 am

    Is there a SQL stored procedure I can run in SQL 2000 that will give me the username of the windows user currently logged in?

    Reply
  • sp_who can solve ur problem

    Reply
  • You can also try:

    SELECT SUSER_SNAME()

    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.

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

    Reply
  • “SELECT @@version” will show you what version of SQL and Windows you are running.

    Reply
  • Ali Tabatabaei
    February 8, 2009 4:38 am

    Tnx a lot for your Post!!! I did use it in one of my Triggers.


    declare @uName char(30);
    declare @Message char(250);
    SELECT @uName=SYSTEM_USER;

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

    Reply
  • how do i obtain the role or roles of the logged on user name

    Reply
  • Cleopas, simply use

    EXECUTE AS USER = ‘username’

    and to return back to your security context, use

    REVERT

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

    Reply
  • I Searched and searched and finally found the RIGHT answer…

    use ORGINAL_LOGIN()

    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.

    Reply
  • Aaron Thomason
    November 3, 2009 3:47 am

    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?

    Reply
  • Hi

    Can you tell me how can I get the password expiration information of a active directory windows user from sql server 2005

    Reply
  • thank you!

    Reply
  • “show user;” returns the current user name

    Reply
  • Chandrashekar.M.S
    May 27, 2010 9:33 pm

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

    Reply
  • Chandrashekar.M.S
    May 28, 2010 11:27 am

    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…

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

      Reply
  • hi pinal

    i need query for all user in a particular database and also number of login also.

    Thanks
    Neelesh

    Reply
  • Thanks!
    Great information, I’ll get it on my triggers.

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

    Reply

Leave a Reply