# Use global variable in data file query

**URL:** <https://forum.katalon.com/t/use-global-variable-in-data-file-query/47697>\
**Category:** Web Testing\
**Tags:** katalon-studio\
**Created:** [September 24, 2020, 6:29pm UTC](https://forum.katalon.com/t/use-global-variable-in-data-file-query/47697 "2020-09-24T18:29:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dpackwood](https://avatars.discourse-cdn.com/v4/letter/d/e95f7d/32.png) [@dpackwood](https://forum.katalon.com/u/dpackwood)\
**Post date:** [September 24, 2020, 6:29pm UTC](https://forum.katalon.com/t/use-global-variable-in-data-file-query/47697/1 "2020-09-24T18:29:39Z")

</div>

I have a test case that is being called from 2 different test suites. I would like to be able to create a data file, with a query that looks like this…

Select \* FROM my\_database\_table WHERE test\_suite = ${GlobalVariable.Test\_Suite}

where the test\_suite value would be replaced with whatever the name of the current test Suite is. It appears that datafiles are read as strings, so the globalVariable is not replaced with the appropriate value from my profile.

has anyone found a work-around for this? Is it possible to use a global variable in a data file query?

---

<div class="post-metadata">

**Author:** ![grylion54](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/grylion54/32/16265_2.png) [@grylion54](https://forum.katalon.com/u/grylion54)\
**Post date:** [September 24, 2020, 6:40pm UTC](https://forum.katalon.com/t/use-global-variable-in-data-file-query/47697/2 "2020-09-24T18:40:17Z")

</div>

@dpackwood I may not understand your question completely, but the query is just a string so you can concatenate any part of your query to your heart’s content.

def query = "Select \* FROM my\_database\_table WHERE " + GV.gTestSuite + " = " + ${GlobalVariable.Test\_Suite}

println query

Are you setting your GV in your TestSuite’s @SetupTestCase method?

---

<div class="post-metadata">

**Author:** ![dpackwood](https://avatars.discourse-cdn.com/v4/letter/d/e95f7d/32.png) [@dpackwood](https://forum.katalon.com/u/dpackwood)\
**Post date:** [September 24, 2020, 6:47pm UTC](https://forum.katalon.com/t/use-global-variable-in-data-file-query/47697/3 "2020-09-24T18:47:14Z")

</div>

@grylion54 thanks for the reply. I’m talking specifically about creating a new ‘Test Data’ File in Katalon Studio. We are creating a database type file, and it asks for an SQL query. Trying to add any “code” values results in an incorrect sql syntax error

We are trying to determine if we can use a global variable in the query text, to create a dynamic query that would pull data based on the test suite name.

---

<div class="post-metadata">

**Author:** ![grylion54](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/grylion54/32/16265_2.png) [@grylion54](https://forum.katalon.com/u/grylion54)\
**Post date:** [September 24, 2020, 7:14pm UTC](https://forum.katalon.com/t/use-global-variable-in-data-file-query/47697/4 "2020-09-24T19:14:11Z")

</div>

If you use GV, you may have to add “toString()” at the end as the GV may be treated as an Object but you want it as a String.

Example:  
GlobalVariable.gHostName.toString()

And if you have an Integer code value, you would need to put quotes around it within the SQL clause (put a single quote just before the double quote and again after your code put double quote and then a single quote to enclose the code value).

---

<div class="post-metadata">

**Author:** ![mariana\_kolko](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/mariana_kolko/32/16103_2.png) [@mariana\_kolko](https://forum.katalon.com/u/mariana_kolko)\
**Post date:** [May 28, 2021, 5:04am UTC](https://forum.katalon.com/t/use-global-variable-in-data-file-query/47697/5 "2021-05-28T05:04:59Z")

</div>

@dpackwood please advise if the solution given by @grylion54 has worked, since I’m facing the same issue, but I have created data files for each table in the db, then the global variable is reading from these data files table, but in the test case when I want to calculate for example globalVariable A (0.5)+ globalVariable B (11.0), the numbers are being added to each other as if they are strings ex the results that I am getting is 0.511.0, and the numbers are not being calculated properly

---

<div class="post-metadata">

**Author:** ![grylion54](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/grylion54/32/16265_2.png) [@grylion54](https://forum.katalon.com/u/grylion54)\
**Post date:** [May 28, 2021, 2:32pm UTC](https://forum.katalon.com/t/use-global-variable-in-data-file-query/47697/6 "2021-05-28T14:32:51Z")

</div>

You can change a String that contains a number into a number that you can do math with by conversion. In your case, you want a “double”. A Double is a number with a decimal point.

```auto
def dnum = Double.parseDouble(GlobalVariable.A) + Double.parseDouble(GlobalVariable.B);

println("the result is " + dnum.toString());

```

> **[Java Convert String to Double examples](https://beginnersbook.com/2013/12/how-to-convert-string-to-double-in-java/)**
>
> Java convert String to Double: In this tutorial we will see the String to Double conversion. There are three ways to convert a String to double value.

Note: An Integer is a number that does not have a decimal point. You can convert a String into an Integer as well, however, you need another method–Integer.parseInt(str).
