Return the Current Workstation Name that’s Connected to SQL Server (T-SQL)

Occasionally you might find yourself in the situation where you need to get the name of the current workstation that’s connected to SQL Server.

For example, maybe you have a stored procedure that inserts data, and you want to record the name of the workstation that inserted the data.

In such cases, you can use the HOST_NAME() function.

This is not to be confused with getting the server name.

Example

Here’s an example of running an ad-hoc query to select the current computer’s name.

SELECT HOST_NAME();

Result:

Tims-MacBook-Pro

In this case, Tim ran the query from his MacBook Pro, and the name of his computer is Tims-MacBook-Pro.

You can probably imagine how handy this could be if you wanted to be able to look at a row and see who inserted that row and from which workstation.

Not for Security Purposes

As handy as this function is, Microsoft warns against using it for security purposes. The reasoning behind this is that, HOST_NAME() is provided by the client, which could possibly provide inaccurate data.

For example a hacker could gain access by spoofing the hostname and that would obviously provide a false record of the workstation being used at the time of the attack.

Recording the Hostname in a Database

Here’s a simple example of creating a table that records the hostname as a default value in a column.

CREATE TABLE Books  
   (
    BookID int IDENTITY(1,1) NOT NULL PRIMARY KEY,  
    BookName nvarchar(255) NOT NULL,  
    Workstation nvarchar(50)  NOT NULL DEFAULT HOST_NAME()
   );
INSERT INTO Books (BookName)
VALUES ('How to Spoof Hostnames');
SELECT * FROM Books;

Result:

+----------+------------------------+---------------------+
 | BookID   | BookName               | Workstation         |
 |----------+------------------------+---------------------|
 | 1        | How to Spoof Hostnames | Tims-MacBook-Pro    |
 +----------+------------------------+---------------------+ 

In this case I used DEFAULT to specify that the hostname is the default value for the row. This saves the application from having to insert this value (but it also doesn’t prevent the application from overriding it).

INSERT INTO Books (BookName, Workstation)
VALUES ('How to Override Hostnames', 'Bobs-Windows-Machine!');
SELECT * FROM Books;

Result:

+----------+---------------------------+-----------------------+
 | BookID   | BookName                  | Workstation           |
 |----------+---------------------------+-----------------------|
 | 1        | How to Spoof Hostnames    | Tims-MacBook-Pro      |
 | 2        | How to Override Hostnames | Bobs-Windows-Machine! |
 +----------+---------------------------+-----------------------+ 

Include the User

If you’re going to record the workstation, then you’d probably also want to include the logged in user. You can use SUSER_NAME() for that.

SELECT 
 HOST_NAME() AS HOST_NAME,
 SUSER_NAME() AS SUSER_NAME;

Result:

+---------------------+--------------+
 | HOST_NAME           | SUSER_NAME   |
 |---------------------+--------------|
 | Tims-MacBook-Pro.   | sa           |
 +---------------------+--------------+