# Get SQL query results as variables

**URL:** <https://forum.katalon.com/t/get-sql-query-results-as-variables/10966>\
**Category:** Integrations\
**Tags:** integrations, katalon-studio\
**Created:** [May 16, 2018, 12:34pm UTC](https://forum.katalon.com/t/get-sql-query-results-as-variables/10966 "2018-05-16T12:34:43Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anthony](https://avatars.discourse-cdn.com/v4/letter/a/49beb7/32.png) [@Anthony](https://forum.katalon.com/u/Anthony)\
**Post date:** [May 16, 2018, 12:34pm UTC](https://forum.katalon.com/t/get-sql-query-results-as-variables/10966/1 "2018-05-16T12:34:43Z")

</div>

Hello,

I am using the following code below to query a MS MSQL Server, and do not know how to deal with the results correctly…

```auto
CustomKeywords.'com.db.sql.connectDB'(server, port, dbname, username, password)

result = CustomKeywords.'com.db.sql.executeQuery'(query)

CustomKeywords.'com.db.sql.closeDatabaseConnection'()

```

The above is working (as in performing the query) using the example keywords from [here](http://forum.katalon.com/discussion/3662/help-with-sql-database-connection--script "Link: http://forum.katalon.com/discussion/3662/help-with-sql-database-connection--script"), but how do I get the result of the query in a useable form.

If after running the above I do:

```auto
println result

```

Then all I get in the console is this:

> SQLServerResultSet:1

How do I see/get at my results? I want to assign them to variables for comparison against others.

Thank you

Anthony

---

<div class="post-metadata">

**Author:** ![Anthony](https://avatars.discourse-cdn.com/v4/letter/a/49beb7/32.png) [@Anthony](https://forum.katalon.com/u/Anthony)\
**Post date:** [May 18, 2018, 11:53am UTC](https://forum.katalon.com/t/get-sql-query-results-as-variables/10966/2 "2018-05-18T11:53:28Z")

</div>

Found a solution to get my results as variables.

Using the built in [data files functionalit](https://docs.katalon.com/display/KD/Manage+Test+Data)y in Katalon Studio, I can create a data file connecting to my DB and then return the results as a variable with the below:

```auto
//get data
def data = findTestData('Data Files/DB Data')
// Get specific data (column, row) as a variable
String record = data.getValue(3, 4)
//print my variable
println record

```

I can then do what ever I need with the variable 😃

---

<div class="post-metadata">

**Author:** ![Justin\_Harper](https://avatars.discourse-cdn.com/v4/letter/j/f6c823/32.png) [@Justin\_Harper](https://forum.katalon.com/u/Justin_Harper)\
**Post date:** [June 13, 2018, 3:57pm UTC](https://forum.katalon.com/t/get-sql-query-results-as-variables/10966/3 "2018-06-13T15:57:01Z")

</div>

Hello, could you please explain a little further? I am trying to achieve the same thing but am unsure how to implement the Data files you have mentioned:

```auto
CustomKeywords.'customMethods.DatabaseMethods.connectDB'('serverAddress', 'TableName', '1433', 'username', 'password')
String queryResult = CustomKeywords.'customMethods.DatabaseMethods.executeQuery'('SELECT STATUSDESCRIPTION FROM dbo.RASPOTSTATUS WHERE SPOTSTATUSID = 15')
CustomKeywords.'customMethods.DatabaseMethods.closeDatabaseConnection'()
println(queryResult)

```

---

<div class="post-metadata">

**Author:** ![Anthony](https://avatars.discourse-cdn.com/v4/letter/a/49beb7/32.png) [@Anthony](https://forum.katalon.com/u/Anthony)\
**Post date:** [June 13, 2018, 7:09pm UTC](https://forum.katalon.com/t/get-sql-query-results-as-variables/10966/4 "2018-06-13T19:09:01Z")

</div>

> Justin Harper said:
> 
> Hello, could you please explain a little further? I am trying to achieve the same thing but am unsure how to implement the Data files you have mentioned:
> 
> ```auto
> CustomKeywords.'customMethods.DatabaseMethods.connectDB'('serverAddress', 'TableName', '1433', 'username', 'password')
> 
> ```

String queryResult = CustomKeywords.‘customMethods.DatabaseMethods.executeQuery’(‘SELECT STATUSDESCRIPTION FROM dbo.RASPOTSTATUS WHERE SPOTSTATUSID = 15’)  
CustomKeywords.‘customMethods.DatabaseMethods.closeDatabaseConnection’()  
println(queryResult)

> ```auto
> 
>   
>   
>   
>   
>   
>   
>   
> 
>   
> 
> ```

Hello Justin, to generate my data file I followed the steps here:  
[https://docs.katalon.com/display/KD/Manage+Test+Data](https://docs.katalon.com/display/KD/Manage+Test+Data "Link: https://docs.katalon.com/display/KD/Manage+Test+Data")doing a select \* from table name query.

Let me know if that is not enough

---

<div class="post-metadata">

**Author:** ![Justin\_Harper](https://avatars.discourse-cdn.com/v4/letter/j/f6c823/32.png) [@Justin\_Harper](https://forum.katalon.com/u/Justin_Harper)\
**Post date:** [June 21, 2018, 2:23pm UTC](https://forum.katalon.com/t/get-sql-query-results-as-variables/10966/5 "2018-06-21T14:23:22Z")

</div>

Thank you for your reply Anthony. That way does work for me and in certain scenarios, is the right solution.

However, in my current scenario, I want to just be able to access the result as a single variable for comparison. Is there a way to do this without having to create a Data file? I would like to just be able to do this within the scripting view of Katalon

This is a high level description of the test I am trying to do:  
1. Login to application front end  
2. Create an ‘order’  
3. Get a unique ID/Order number  
4. Query the database to check the order has been created correctly with unique id  
5. Make changes to the order in the application front end  
6. Query the database again to make sure updates have been committed correctly

So something like

```auto
CustomKeywords.'customMethods.DatabaseMethods.connectDB'('serverAddress', 'TableName', '1433', 'username', 'password')
String queryResult = CustomKeywords.'customMethods.DatabaseMethods.executeQuery'('SELECT STATUSDESCRIPTION FROM dbo.RASPOTSTATUS WHERE SPOTSTATUSID = 15')
CustomKeywords.'customMethods.DatabaseMethods.closeDatabaseConnection'()String expectedResult = "Update"
//Then compare queryResult against expectedResult and pass/fail the test based on this

```

```auto
Does that make sense?

```

---

<div class="post-metadata">

**Author:** ![Anthony](https://avatars.discourse-cdn.com/v4/letter/a/49beb7/32.png) [@Anthony](https://forum.katalon.com/u/Anthony)\
**Post date:** [June 23, 2018, 11:39pm UTC](https://forum.katalon.com/t/get-sql-query-results-as-variables/10966/6 "2018-06-23T23:39:48Z")

</div>

> Justin Harper said:
> 
> Thank you for your reply Anthony. That way does work for me and in certain scenarios, is the right solution.
> 
> However, in my current scenario, I want to just be able to access the result as a single variable for comparison. Is there a way to do this without having to create a Data file? I would like to just be able to do this within the scripting view of Katalon
> 
> This is a high level description of the test I am trying to do:  
> 1. Login to application front end  
> 2. Create an ‘order’  
> 3. Get a unique ID/Order number  
> 4. Query the database to check the order has been created correctly with unique id  
> 5. Make changes to the order in the application front end  
> 6. Query the database again to make sure updates have been committed correctly
> 
> So something like
> 
> ```auto
> CustomKeywords.'customMethods.DatabaseMethods.connectDB'('serverAddress', 'TableName', '1433', 'username', 'password')
> 
> ```

String queryResult = CustomKeywords.‘customMethods.DatabaseMethods.executeQuery’(‘SELECT STATUSDESCRIPTION FROM dbo.RASPOTSTATUS WHERE SPOTSTATUSID = 15’)  
CustomKeywords.‘customMethods.DatabaseMethods.closeDatabaseConnection’()String expectedResult = “Update”  
//Then compare queryResult against expectedResult and pass/fail the test based on this

> ```auto
> 
>   
>   
> 
> ```
> 
> Does that make sense?
> 
> ```auto
> 
>   
> 
> ```

Yes, that makes sense and is simular to what I was trying to acheive when I first posted.

But I did not know how to deal with the results, leading me to use the data file as a quick and simple work around.

Anyway here: [https://dzone.com/articles/how-to-perform-database-testing-using-katalon-stud](https://dzone.com/articles/how-to-perform-database-testing-using-katalon-stud "Link: https://dzone.com/articles/how-to-perform-database-testing-using-katalon-stud") is what I used as an example to script my SQL connection and query, hopefully this will help you.

If you can figure out how to deal with query results as variables please share, it would be useful.

---

<div class="post-metadata">

**Author:** ![Justin\_Harper](https://avatars.discourse-cdn.com/v4/letter/j/f6c823/32.png) [@Justin\_Harper](https://forum.katalon.com/u/Justin_Harper)\
**Post date:** [June 28, 2018, 2:13pm UTC](https://forum.katalon.com/t/get-sql-query-results-as-variables/10966/7 "2018-06-28T14:13:23Z")

</div>

Got it:)

A Resultset is a table of data representing the results we get back from the query. A cursor is maintained inside the ResultSet but is not always set to the current row (i.e. the row with our data)

We ned to move the cursor first, then call the getString method to get our string. Probably not the best explanation but these sites do it better:  
[https://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html](https://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html "Link: https://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html")  
[https://docs.oracle.com/javase/tutorial/jdbc/basics/retrieving.html#retrieve\_rs](https://docs.oracle.com/javase/tutorial/jdbc/basics/retrieving.html#retrieve_rs "Link: https://docs.oracle.com/javase/tutorial/jdbc/basics/retrieving.html#retrieve\_rs")  
[http://websitenotebook.blogspot.com/2010/06/resultset-has-no-current-row.html](http://websitenotebook.blogspot.com/2010/06/resultset-has-no-current-row.html "Link: http://websitenotebook.blogspot.com/2010/06/resultset-has-no-current-row.html") (This one really drove it home for me)

So your code would look something like this. NOTE that you need to keep the connection open whilst you manipulate the ResultSet otherwise it has nothing to manipulate and will return an error:

```auto
//Connect to Database
CustomKeywords.'customMethods.DatabaseMethods.connectDB'('IPAddress', 'tableName', '1433', 'username', 'password')
//Execute our query
ResultSet queryResult = CustomKeywords.'customMethods.DatabaseMethods.executeQuery'('SELECT STATUSDESCRIPTION FROM dbo.RASPOTSTATUS WHERE SPOTSTATUSID = 15')
//Move the cursor in the ResultSet table to the first row
queryResult.next()
//Save the result as a string
String valueFromDatabase = queryResult.getString(1)
//Print our result from the database as a string to the console
println(valueFromDatabase)
//Close the connection
CustomKeywords.'customMethods.DatabaseMethods.closeDatabaseConnection'()

```

---

<div class="post-metadata">

**Author:** ![Anthony](https://avatars.discourse-cdn.com/v4/letter/a/49beb7/32.png) [@Anthony](https://forum.katalon.com/u/Anthony)\
**Post date:** [June 28, 2018, 8:23pm UTC](https://forum.katalon.com/t/get-sql-query-results-as-variables/10966/8 "2018-06-28T20:23:17Z")

</div>

> [Justin Harper](https://forum.katalon.com/profile/4252/Justin%20Harper) said:
> 
> Got it:)
> 
> A Resultset is a table of data representing the results we get back from the query. A cursor is maintained inside the ResultSet but is not always set to the current row (i.e. the row with our data)
> 
> We ned to move the cursor first, then call the getString method to get our string. Probably not the best explanation but these sites do it better:  
> [https://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html](https://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html "Link: https://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html")  
> [https://docs.oracle.com/javase/tutorial/jdbc/basics/retrieving.html#retrieve\_rs](https://docs.oracle.com/javase/tutorial/jdbc/basics/retrieving.html#retrieve_rs "Link: https://docs.oracle.com/javase/tutorial/jdbc/basics/retrieving.html#retrieve\_rs")  
> [http://websitenotebook.blogspot.com/2010/06/resultset-has-no-current-row.html](http://websitenotebook.blogspot.com/2010/06/resultset-has-no-current-row.html "Link: http://websitenotebook.blogspot.com/2010/06/resultset-has-no-current-row.html") (This one really drove it home for me)
> 
> So your code would look something like this. NOTE that you need to keep the connection open whilst you manipulate the ResultSet otherwise it has nothing to manipulate and will return an error:
> 
> ```auto
> //Connect to Database
> 
> ```

CustomKeywords.‘customMethods.DatabaseMethods.connectDB’(‘IPAddress’, ‘tableName’, ‘1433’, ‘username’, ‘password’)  
//Execute our query  
ResultSet queryResult = CustomKeywords.‘customMethods.DatabaseMethods.executeQuery’(‘SELECT STATUSDESCRIPTION FROM dbo.RASPOTSTATUS WHERE SPOTSTATUSID = 15’)  
//Move the cursor in the ResultSet table to the first row  
queryResult.next()  
//Save the result as a string  
String valueFromDatabase = queryResult.getString(1)  
//Print our result from the database as a string to the console  
println(valueFromDatabase)  
//Close the connection  
CustomKeywords.‘customMethods.DatabaseMethods.closeDatabaseConnection’()

> ```auto
> 
>   
>   
>   
>   
>   
>   
>   
>   
>   
> 
>   
> 
> ```

That is great, thanks for sharing what you found!

---

<div class="post-metadata">

**Author:** ![Drunda\_Nibel](https://avatars.discourse-cdn.com/v4/letter/d/bb73d2/32.png) [@Drunda\_Nibel](https://forum.katalon.com/u/Drunda_Nibel)\
**Post date:** [October 23, 2018, 10:21am UTC](https://forum.katalon.com/t/get-sql-query-results-as-variables/10966/9 "2018-10-23T10:21:25Z")

</div>

Take a closer look at [this post](http://forum.katalon.com/discussion/5041/error-on-database-testing-communications-link-failure#Comment_24595) to avoid this cumbersome handling of cursors (or pointers) for ResultSet objects. When using the groovy.sql.Sql class, you only need one Keyword method to create the desired SQL instance. Then their methods can be used directly to apply all required SQL statements and process their result sets as maps or lists.

---

<div class="post-metadata">

**Author:** ![Justin\_Harper](https://avatars.discourse-cdn.com/v4/letter/j/f6c823/32.png) [@Justin\_Harper](https://forum.katalon.com/u/Justin_Harper)\
**Post date:** [April 25, 2019, 2:29pm UTC](https://forum.katalon.com/t/get-sql-query-results-as-variables/10966/10 "2019-04-25T14:29:57Z")

</div>

Hi Drunda,

Could you please repost the link? It doesn’t seem to work anymore.

Thanks,  
Justin

---

<div class="post-metadata">

**Author:** ![Drunda\_Nibel](https://avatars.discourse-cdn.com/v4/letter/d/bb73d2/32.png) [@Drunda\_Nibel](https://forum.katalon.com/u/Drunda_Nibel)\
**Post date:** [April 27, 2019, 1:23pm UTC](https://forum.katalon.com/t/get-sql-query-results-as-variables/10966/11 "2019-04-27T13:23:56Z")

</div>

@Justin_Harper:

Yes, with pleasure. 😉 The new direct link is: [Error on database testing: communications link failure](http://forum.katalon.com/t/error-on-database-testing-communications-link-failure/9219/7?u=drunda_nibel)

---

<div class="post-metadata">

**Author:** ![shilpi1](https://avatars.discourse-cdn.com/v4/letter/s/b3f665/32.png) [@shilpi1](https://forum.katalon.com/u/shilpi1)\
**Post date:** [August 20, 2019, 10:42am UTC](https://forum.katalon.com/t/get-sql-query-results-as-variables/10966/12 "2019-08-20T10:42:18Z")

</div>

how to do update query ??

---

<div class="post-metadata">

**Author:** ![hemalatha.b](https://avatars.discourse-cdn.com/v4/letter/h/ecb155/32.png) [@hemalatha.b](https://forum.katalon.com/u/hemalatha.b)\
**Post date:** [August 19, 2020, 5:51am UTC](https://forum.katalon.com/t/get-sql-query-results-as-variables/10966/13 "2020-08-19T05:51:46Z")

</div>

Pls any one help me.how can be connect odbc database in katalon studio
