Get Terminal Server Client Name via T-SQL

Posted on 2009-12-27
Last Modified: 2012-06-21
I have a SQL Server application running on a citrix platform I need to know the client machine name in SQL Server,

i tried using the statement:

xp_cmdshell 'ECHO %CLIENTNAME%' and it returns null, strangely when I run this command in the cmd window i do get the desired value

when i run the SET command it lists me all the environment values but clientname is not there amongst other values, when i run it in the commant prompt window i do see more values including the clientname.

I'm open to any other suggestion how to get the client name

Question by:Mendel_Lowy
    LVL 26

    Expert Comment


    SELECT @@servername

    Author Comment

    thanks, but that's no good, i'm looking for the client machine name that's logged on to the terminal/citrix server, not the server name
    LVL 26

    Expert Comment

    When you are using xp_cmdshell stored procedure it will always run ON THE SQL server and not on the client you are calling it from. The connection to the SQL database server sends the command to the server and is executed on that machine.

    i.e if you invoke:

    EXEC master..xp_cmdshell 'dir c:\'

    will return the content of the C:\ driver from the machine where the SQL server is running.

    In conclusion If you tried to used xp_cmdshel to do whatever you were doing the only conclusion was that you actually were interested in information from the SQL server box and to get that machine's name you execute SELECT @@servername.

    If you try to get the name of the box where your application is running than there is no way you can use SQL code. Try some functions in the language that your application is written in.

    Author Comment


    I see! it makes absolutly sense!

    I'll tell you even more, I tried to query the registry via xp_regread, because I found that there is a key in the HKEY_CURRENT_USER hive that has the CLIENTNAME, but to my surprise it returned nothing, while I saw it physicaly that key, it drove me crazy

    but with your logic, probably since its excuting the command on sql server itself, that key is probably not present. as a matter of fact the entire HKEY_CURRENT_USER  is not present

    thanks for the enlightment!
    LVL 31

    Expert Comment

    by:Henrik Johansson
    The stored procedures xp* are running on the SQL server, not client
    SQL isn't running in the same session as the TS-connection, so you don't get the client's HKCU or session specific variables.
    Let the application connecting to SQL server retrieve the variable and submit the value as a parameter to the stored procedure executing the query.
    LVL 26

    Accepted Solution

    All the extended stored procedures in SQL server are executed on the hosting machine where SQL server resides. Remember, applications talk to the SQL server through connections by sending commands to that machine and getting back responses from it. There are 2 different environments we are talking about here unless the application is actually on the same machine with the SQL server, which usually isn't.

    If you need to store the client's name in the SQL database then you need to find that name from the application that runs on the client and then pass it as a parameter to the SQL database and store it. You cannot do that using any SQL statement but for sure you can do it from your application. You only need to find the right function or system variable call that returns that.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
    Learn about cloud computing and its benefits for small business owners.
    How to install and configure Citrix XenApp 6.5 - Part 1. In this video tutorial we have explained step by step installation of Citrix XenApp 6.5 Server on Windows Server 2008 R2 is explained in this video. We have explained the difference between…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now