Return SQL Server service user in T-SQL

  • Hi all

    Is it possible to get the name of the user running the 'current' SQL Server instance in T-SQL?

    I know that pretty much everything is possible, so the question will no doubt become: 'What is the best way to do this?'

    Thanks

    Phil


  • Assuming I understand you correctly

    SELECT SUSER_NAME()


    Forever trying to learn
    My blog - http://www.cadavre.co.uk/
    For better, quicker answers on T-SQL questions, click on the following...http://www.sqlservercentral.com/articles/Best+Practices/61537/
    For better, quicker answers on SQL Server performance related questions, click on the following...http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • Thanks, but that returns the current user - not quite what I am after.

    I want the name of the user which is running the SQL Server service for the particular instance on which the query is executing, on the DB server itself.


  • Phil Parkin (12/12/2011)


    Thanks, but that returns the current user - not quite what I am after.

    I want the name of the user which is running the SQL Server service for the particular instance on which the query is executing, on the DB server itself.

    Ah, so I didn't understand correctly 😀

    I don't think you can return that information with T-SQL (happy to be proven incorrect). I'd guess a CLR may be able to do it, is that an option?


    Forever trying to learn
    My blog - http://www.cadavre.co.uk/
    For better, quicker answers on T-SQL questions, click on the following...http://www.sqlservercentral.com/articles/Best+Practices/61537/
    For better, quicker answers on SQL Server performance related questions, click on the following...http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • Cadavre (12/12/2011)


    Phil Parkin (12/12/2011)


    Thanks, but that returns the current user - not quite what I am after.

    I want the name of the user which is running the SQL Server service for the particular instance on which the query is executing, on the DB server itself.

    Ah, so I didn't understand correctly 😀

    I don't think you can return that information with T-SQL (happy to be proven incorrect). I'd guess a CLR may be able to do it, is that an option?

    Thanks for that - CLR is an option, but overkill in this case. It's just a one-off job and it's easier just to tweak the definition of the proc from one server to another than worry about CLRs.

    --edit I just reread my original post - sorry I wasn't quite specific enough with the question.


  • This should do the job:

    http://www.sqldbadiaries.com/2010/10/23/query-to-find-out-service-account-details/

    The query is for the default instance, but is easily adapted for named instances

  • Thank you Howard!


Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply