SQL Server Performance

Retreive Windows user logged on PC, possible?

Discussion in 'SQL Server 2005 T-SQL Performance Tuning' started by Ismailc, Apr 20, 2009.

  1. Ismailc New Member

    Hi,
    I would like to know if it's possible to retreive Windows user logged on to the PC (domainuserid) using Sql, if possible please provide code?
    Regards
  2. MohammedU New Member

    Question is not clear...can you post explanation...
  3. Sandy New Member

    [quote user="Ismailc"]
    Hi,
    I would like to know if it's possible to retreive Windows user logged on to the PC (domainuserid) using Sql, if possible please provide code?
    Regards
    [/quote]
    Check this...DECLARE @chvDomainName NVARCHAR(100)
    EXEC master.dbo.xp_regread 'HKEY_LOCAL_MACHINE',
    'SOFTWAREMicrosoftWindows NTCurrentVersionWinlogon',
    N'CachePrimaryDomain',@chvDomainName OUTPUT
    SELECT @chvDomainName AS DomainName
    if you wants the other info please query this: sp_who
    Thanks,
    Sandy.
  4. Sandy New Member

    Hi Ismailc,
    here is your script......./* Dimain Name */DECLARE
    @chvDomainName NVARCHAR(100) EXEC
    master.dbo.xp_regread 'HKEY_LOCAL_MACHINE', 'SOFTWAREMicrosoftWindows NTCurrentVersionWinlogon', N
    'CachePrimaryDomain',@chvDomainName OUTPUT SELECT
    @chvDomainName AS DomainName/* User Name */DECLARE
    @UserName NVARCHAR(100) EXEC
    master.dbo.xp_regread 'HKEY_LOCAL_MACHINE', 'SOFTWAREMicrosoftWindows NTCurrentVersionWinlogon', N
    'DefaultUsername',@UserName OUTPUT SELECT
    @UserName AS UserName
    Thanks,
    Sandy.
  5. Ismailc New Member

    Great stuff - Thank You All.
    I did not think this was possible & was thinking of other ideas to get the PC user logged on.
    Unfortunately the query works but not completely the way i need it to be.
    The query returns the login details of the user logged on to the SQL server PC eg the DB lies on a server and it retreives the user logged on the the server.
    I'm looking to retreive the information from the user's PC executing the script on the server.
    eg. It must return my details running the script & not the user on the server.
    Regards
  6. Adriaan New Member

    Only if the user is logged on to SQL with Windows authentication ...select
    x.nt_domain, x.nt_user_name
    from sys.dm_exec_sessions x
    where x.session_id = @@SPID
    Otherwise, it will have to come from the client app.
  7. yorkw New Member

    If you're looking for the user actually executing the script or stored procedure can't you just use system_user
    select system_user
    ???
  8. Ismailc New Member

    Hi,
    I got the userid using the web application in server side code,
    system_user returns the id logged onto the database & not the NT user.
    Thank You All
  9. FrankKalis Moderator

    For a web application you need to have the application coded in a way that it sends you the username for the user who connecting through a browser. SQL Server has no way of knowing it when you use SQL authentication.
  10. Ismailc New Member

    Hi,
    I created an SSRS report with a parameter based on the userid the wep application returns.
    Regards
  11. FrankKalis Moderator

    That's what I do here as well. Inside the db then I check the rest based on what RS supplies me with. [:)]

Share This Page