none
How to find windows user name in SQL server 2008

    Question

  • Hi Experts,

     need to get the Windows account user id of the user who is running the sql
    query. Currently I'm using the following statement,
    SELECT @UserName = SYSTEM_USER
    However, if the user run the statement using SQL account, I'd get the SQL
    account login name, instead of the Windows account user id.

    Is there any way that I can get the Windows account user id, no matter the
    user login using Windows authentication or SQL authentication?

    • Edited by Sweet2010 Tuesday, November 09, 2010 1:12 PM changed
    Tuesday, November 09, 2010 1:12 PM

Answers

  • Hello, Abdul

    There is no built-in way of finding the Windows user name using T-SQL, if the connection is made using SQL Authentication. If it's really needed, the application would have to determine it on the client-side and comunicate it to the server.

    Razvan

    Tuesday, November 09, 2010 1:21 PM

All replies

  • You mean a user has logged in as a sql server account and you want to find the windows credentials that they are logged in to a domain with (which may not be the same domain that the sql server resides in).

     

    Tuesday, November 09, 2010 1:20 PM
  • Hello, Abdul

    There is no built-in way of finding the Windows user name using T-SQL, if the connection is made using SQL Authentication. If it's really needed, the application would have to determine it on the client-side and comunicate it to the server.

    Razvan

    Tuesday, November 09, 2010 1:21 PM
  • Hello, try this:

    SELECT
     HOST_NAME
    () AS
     HostName
    , SUSER_NAME
    () LoggedUser
    
    You can also create an log table and create DDL Triggers on those tables you want to check who is changing.

    Hope this helps.

     


    Willy Taveras.-

    http://itlearn.net

    Tuesday, November 09, 2010 1:26 PM
  • hi,

    This shows only current SQL username only.

    Pls help me to find out the solution.

    Thanks,

    Abdul

     

    Tuesday, November 09, 2010 1:31 PM
  • hi,

    This shows only current SQL username only.

    Pls help me to find out the solution.

    Thanks,

    Abdul

     


    There isn't one. You would have to find somethinm gto trace the login back to the originator then find out how they logged in to that domain. That may be possible if you could follow back through routers and such but would indicate a security hole and probably be dependent on the network setup.
    Tuesday, November 09, 2010 1:35 PM
  • Hi,

     how windows username stored in  ExecutionLogEntry  in sql 2008 reportserver database.

    what is the query used behind this.

    Like this i need  query.

     

    Thanks,

    Abdul

    Tuesday, November 09, 2010 4:13 PM
  • Hi

    if you know the host name run the folllwing PS script to find logged on user's name

    $strComputer

     

    = "XXXXX"

    $Var

     

    =GWMI -Comp $strComputer -CL Win32_ComputerSystem

    "Machine Name: "

    + $Var.Name + " User Name: " + $Var.UserName

    cheers

    Tuesday, November 09, 2010 4:50 PM
  • yes nigel...
    Wednesday, November 10, 2010 7:29 AM
  • could you please explain in T-SQL ..

    Thanks in advance

    Parixitsinh

    Monday, August 13, 2012 7:06 AM