# How to get local server name and use it in a SQL query

**URL:** <https://forums.toadworld.com/t/how-to-get-local-server-name-and-use-it-in-a-sql-query/61371>\
**Category:** Toad Data Point & Toad Intelligence Central\
**Created:** [October 6, 2025, 8:55pm UTC](https://forums.toadworld.com/t/how-to-get-local-server-name-and-use-it-in-a-sql-query/61371 "2025-10-06T20:55:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mariobiron](https://sea1.discourse-cdn.com/quest/user_avatar/forums.toadworld.com/mariobiron/32/165_2.png) [@mariobiron](https://forums.toadworld.com/u/mariobiron)\
**Post date:** [October 6, 2025, 8:55pm UTC](https://forums.toadworld.com/t/how-to-get-local-server-name-and-use-it-in-a-sql-query/61371/1 "2025-10-06T20:55:29Z")

</div>

Hi,

How do I get the name of the machine DP is running while running a script and passing that name to a SQL query to Oracle?

Thank you!

---

<div class="post-metadata">

**Author:** ![mariobiron](https://sea1.discourse-cdn.com/quest/user_avatar/forums.toadworld.com/mariobiron/32/165_2.png) [@mariobiron](https://forums.toadworld.com/u/mariobiron)\
**Post date:** [October 9, 2025, 2:42pm UTC](https://forums.toadworld.com/t/how-to-get-local-server-name-and-use-it-in-a-sql-query/61371/2 "2025-10-09T14:42:14Z")

</div>

I can see 2 ways of getting this info from command line; could any of those be leveraged to set a variable in DP and then use this variable in a SQL query?  
Variation 1: echo %COMPUTERNAME%  
Variation 2: C:\Windows\system32\hostname.exe  
Thank you

---

<div class="post-metadata">

**Author:** ![milos.djordjevic](https://avatars.discourse-cdn.com/v4/letter/m/7ba0ec/32.png) [@milos.djordjevic](https://forums.toadworld.com/u/milos.djordjevic)\
**Post date:** [October 20, 2025, 3:13pm UTC](https://forums.toadworld.com/t/how-to-get-local-server-name-and-use-it-in-a-sql-query/61371/3 "2025-10-20T15:13:37Z")

</div>

Hello,

TDP doesn’t have built-in variables that will return the hostname of the machine, this is something that can be done via SQL inside of you Oracle script.

However, depending on you use case, it might be possible to achive this with a Toad Automation Variable.

Open Toad Automation, and select the Set Variable step from System Activities. Then set the variable name and specify SQL as the type for the variable.

Then in the editor that is displayed, enter the SQL that would extract the hostname of the machine where the Automation script is running:

 ![image](https://global.discourse-cdn.com/quest/original/3X/c/c/cce9e92d0d4bf392a16fea720728f35cc833ee29.png)

Then, use this variable in a SQL script that you are executing. Choose an appropriate step that meets your needs. In this example, I am using the Execute Script automation step from Database activities, and I am using the variable in an Insert statement:

 ![image](https://global.discourse-cdn.com/quest/original/3X/1/6/160a4770d8e0b117b9584e0efd7eda4402465be0.png)

And this i the output that my steps have created (Inserted the value from the :HOST\_NAME variable into the INSERT\_TEST\_QA table in my example):

 ![image](https://global.discourse-cdn.com/quest/original/3X/3/f/3fd2b028103f9ba049a9927b7cfdc38329011340.png)

NOTE: In this example, I used the following Oracle query with the SYS\_CONTEXT function for returning the hostname of the machine:

```sql
SELECT SUBSTR(SYS_CONTEXT('USERENV', 'HOST'),
              INSTR(SYS_CONTEXT('USERENV', 'HOST'), '\', -1) + 1) AS hostname
FROM DUAL;

```

---

<div class="post-metadata">

**Author:** ![mariobiron](https://sea1.discourse-cdn.com/quest/user_avatar/forums.toadworld.com/mariobiron/32/165_2.png) [@mariobiron](https://forums.toadworld.com/u/mariobiron)\
**Post date:** [October 20, 2025, 6:56pm UTC](https://forums.toadworld.com/t/how-to-get-local-server-name-and-use-it-in-a-sql-query/61371/4 "2025-10-20T18:56:47Z")

</div>

> [@milos.djordjevic](#):
>
> ```auto
> SELECT SUBSTR(SYS_CONTEXT('USERENV', 'HOST'),
> INSTR(SYS_CONTEXT('USERENV', 'HOST'), '\', -1) + 1) AS hostname
> FROM DUAL;
> 
> ```

This is exactly what I needed! Thank you so much!

Marked your answer as Solution
