[Last Call] Learn how to a build a cloud-first strategyRegister Now


Get Terminal Server Client Name via T-SQL

Posted on 2009-12-27
Medium Priority
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
  • 3
  • 2
LVL 27

Expert Comment

ID: 26127590

SELECT @@servername

Author Comment

ID: 26127662
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 27

Expert Comment

ID: 26127680
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.
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.


Author Comment

ID: 26127741

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
ID: 26127803
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 27

Accepted Solution

Zberteoc earned 2000 total points
ID: 26127831
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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This article provides a convenient collection of links to Microsoft provided Security Patches for operating systems that have reached their End of Life support cycle. Included operating systems covered by this article are Windows XP,  Windows Server…
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

830 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