# SQL Query Withing runScript()

**URL:** <https://forum.inductiveautomation.com/t/sql-query-withing-runscript/18528>\
**Category:** Ignition\
**Tags:** ignition79, scripting\
**Created:** [June 19, 2018, 4:04pm UTC](https://forum.inductiveautomation.com/t/sql-query-withing-runscript/18528 "2018-06-19T16:04:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![b\_zacht](https://avatars.discourse-cdn.com/v4/letter/b/da6949/32.png) [@b\_zacht](https://forum.inductiveautomation.com/u/b_zacht)\
**Post date:** [June 19, 2018, 4:04pm UTC](https://forum.inductiveautomation.com/t/sql-query-withing-runscript/18528/1 "2018-06-19T16:04:54Z")

</div>

I have the following script in the global scripts library under the script name ‘zTest’. I am attempting to run this code in two different expressions using `runScript('shared.zTest.myFunc',0)` and `runScript('shared.zTest.myString',0)`. The expressions define parameters for a report.

```auto
def myFunc():
	x = system.db.runQuery('SELECT FormType FROM PMFormClass')
	x = [i[0] for i in x]
	x = [[a] for a in x]
	y = system.dataset.toDataSet(['Form Type'], x)
	return y

def myString():
	return 'Generic String'

```

The call to myString() successfully outputs the desired string, but myFunc() does not, yielding this error:

 ![](https://us1.discourse-cdn.com/inductiveautomation/original/2X/3/356324cf66bf2aac465d1c69052228e693ba68c7.png)

Using a button in Vision Client I can successfully take the return from myFunc() and pass it directly into a table as a dataset, so I believe the SQL query and the code in myFunc() is correct.

After some troubleshooting it appears that runScript() won’t run anything with a SQL query in it. Does anyone have any ideas why this might be?

I know for this very simple case it would be better to use the reporting module’s SQL capability, but this is the very boiled down version of the script that will eventually be in production. There are also a variety of reasons I would like to use the script method if it is possible.

Any ideas would be greatly appreciated.

Ignition Version: 7.9.6  
Java Version (Gateway): 1.8.0\_151-b12  
Java Version (PC): 8.0.1510.12

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [June 19, 2018, 4:12pm UTC](https://forum.inductiveautomation.com/t/sql-query-withing-runscript/18528/2 "2018-06-19T16:12:20Z")

</div>

`system.db` functions need to have a database name specified when run from the gateway scope (ie, when building your report); add a second string argument that’s just the name of your DB connection in Ignition.

---

<div class="post-metadata">

**Author:** ![b\_zacht](https://avatars.discourse-cdn.com/v4/letter/b/da6949/32.png) [@b\_zacht](https://forum.inductiveautomation.com/u/b_zacht)\
**Post date:** [June 19, 2018, 4:17pm UTC](https://forum.inductiveautomation.com/t/sql-query-withing-runscript/18528/3 "2018-06-19T16:17:59Z")

</div>

Works Perfectly! I feel a little silly that it was that simple. Thanks for the help.

---

<div class="post-metadata">

**Author:** ![flavien](https://avatars.discourse-cdn.com/v4/letter/f/f475e1/32.png) [@flavien](https://forum.inductiveautomation.com/u/flavien)\
**Post date:** [November 21, 2018, 8:25am UTC](https://forum.inductiveautomation.com/t/sql-query-withing-runscript/18528/4 "2018-11-21T08:25:57Z")

</div>

Hi,  
In Reports module’s parameters, I would like to link the client tag ‘DefaultDatabase’ to a runQuery function. It’s not working with my code below :

```auto
runScript("system.db.runQuery('SELECT start_datetime FROM overview_winter ORDER BY id DESC LIMIT 1','"+{[System]Client/System/DefaultDatabase}+"')[0][0]")

```

I tried tag() expression but it’s not working too. Can you help me ?

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [November 21, 2018, 4:01pm UTC](https://forum.inductiveautomation.com/t/sql-query-withing-runscript/18528/5 "2018-11-21T16:01:35Z")

</div>

> [@flavien](#):
>
> `{[System]Client/System/DefaultDatabase}`

The Client folder in the System tag provider only exists when you're in the _client_ scope - reports execute on the gateway, without a particular client or project associated with them.
