# SQL runScript in expression binding

**URL:** <https://forum.inductiveautomation.com/t/sql-runscript-in-expression-binding/64505>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [September 26, 2022, 8:58pm UTC](https://forum.inductiveautomation.com/t/sql-runscript-in-expression-binding/64505 "2022-09-26T20:58:07Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![craigb](https://avatars.discourse-cdn.com/v4/letter/c/9f8e36/32.png) [@craigb](https://forum.inductiveautomation.com/u/craigb)\
**Post date:** [September 26, 2022, 8:58pm UTC](https://forum.inductiveautomation.com/t/sql-runscript-in-expression-binding/64505/1 "2022-09-26T20:58:07Z")

</div>

I’m trying to make a quick popup for editing some data in a table

So, user selects a row in the table. That unique value is passed to the popup. The pop up has a few labels that will be pre filled with the data by using that unique value. On the label I added a custom expression binding, I know I can do this with named queries very easy, guess I was just being lazy doing it in a expression, for the amount of time this form will be used.

```auto
system.db.runScalarQuery("SELECT category FROM equipment WHERE serial='BB-001'")

```

The above code works in the script console, I used a fixed Where value as the script console wont know the passed value for testing. That code wont work in the expression binding as I need to use ‘runScript’

```auto
runScript("system.db.runScalarQuery('SELECT category FROM equipment WHERE serial = 'BB-001'')")

```

No matter what I do I cant get that to work. If I remove the where Claus it works and returns the first value. With the where Claus I just cant figure it out, single quotes, double quotes, tried them all (I think). I will eventually change the where Claus to the {view.params.Serial} I just used a fixed value for quick testing.

Any ideas ?

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [September 26, 2022, 9:13pm UTC](https://forum.inductiveautomation.com/t/sql-runscript-in-expression-binding/64505/2 "2022-09-26T21:13:20Z")

</div>

The best idea is to not do that and move it into a named query, as you already stated.

If it’s just for understanding, your double single quotes are the issue, as you’ve also identified. You need to use one of the following:

```auto
runScript("system.db.runScalarQuery(""SELECT category FROM equipment WHERE serial = 'BB-001'"" )")

runScript("system.db.runScalarQuery('SELECT category FROM equipment WHERE serial = ''BB-001''')")

```

---

<div class="post-metadata">

**Author:** ![Matthew.gaitan](https://avatars.discourse-cdn.com/v4/letter/m/59ef9b/32.png) [@Matthew.gaitan](https://forum.inductiveautomation.com/u/Matthew.gaitan)\
**Post date:** [September 26, 2022, 9:16pm UTC](https://forum.inductiveautomation.com/t/sql-runscript-in-expression-binding/64505/3 "2022-09-26T21:16:11Z")

</div>

You’re missing the `database` parameter  
[https://docs.inductiveautomation.com/display/DOC81/system.db.runScalarQuery](https://docs.inductiveautomation.com/display/DOC81/system.db.runScalarQuery)

or Nick might be right… I couldn’t get mine to work without adding the database parameter though. Didn’t pick up on the difference in quotes :o

---

<div class="post-metadata">

**Author:** ![schenk](https://avatars.discourse-cdn.com/v4/letter/s/6bbea6/32.png) [@schenk](https://forum.inductiveautomation.com/u/schenk)\
**Post date:** [September 26, 2022, 9:45pm UTC](https://forum.inductiveautomation.com/t/sql-runscript-in-expression-binding/64505/4 "2022-09-26T21:45:00Z")

</div>

I would switch to a named query either way, but you could also do it this way  
`runScript('system.db.runScalarQuery', 0, "SELECT category FROM equipment WHERE serial = 'BB-001'")`

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [September 26, 2022, 10:29pm UTC](https://forum.inductiveautomation.com/t/sql-runscript-in-expression-binding/64505/5 "2022-09-26T22:29:56Z")

</div>

Ah yes, that too. Gateway scope so it needs to know which database as there’s no default database config for gateway 👍

---

<div class="post-metadata">

**Author:** ![craigb](https://avatars.discourse-cdn.com/v4/letter/c/9f8e36/32.png) [@craigb](https://forum.inductiveautomation.com/u/craigb)\
**Post date:** [September 27, 2022, 1:21pm UTC](https://forum.inductiveautomation.com/t/sql-runscript-in-expression-binding/64505/6 "2022-09-27T13:21:44Z")

</div>

> [@nminchin](#):
>
> The best idea is to not do that and move it into a named query, as you already stated.
> 
> If it’s just for understanding, your double single quotes are the issue, as you’ve also identified. You need to use one of the following:
> 
> ```auto
> runScript("system.db.runScalarQuery(""SELECT category FROM equipment WHERE serial = 'BB-001'"" )")
> 
> runScript("system.db.runScalarQuery('SELECT category FROM equipment WHERE serial = ''BB-001''')")
> 
> ```

Both these gave errors, the top one was a config error and the bottom one an expression error, even when adding the DB name.

The only one that worked was..

```auto
runScript('system.db.runScalarQuery', 0, "SELECT category FROM equipment WHERE serial = 'BB-001'")

```

I'm not sure what the '0' is doing in that expression, or the single quotes around the runScalarQuery. Nothing like that is mentioned in the docs, but it works.

I'll switch to a named query, this is just handy when you want to fire up a quick page.

Thanks all.

---

<div class="post-metadata">

**Author:** ![jlandwerlen](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@jlandwerlen](https://forum.inductiveautomation.com/u/jlandwerlen)\
**Post date:** [September 27, 2022, 1:40pm UTC](https://forum.inductiveautomation.com/t/sql-runscript-in-expression-binding/64505/7 "2022-09-27T13:40:37Z")

</div>

> [@craigb](#):
>
> I'm not sure what the '0' is doing in that expression, or the single quotes around the runScalarQuery. Nothing like that is mentioned in the docs, but it works.

The docs do refer to it, it's the pollrate,  
[https://docs.inductiveautomation.com/display/DOC81/runScript](https://docs.inductiveautomation.com/display/DOC81/runScript)

---

<div class="post-metadata">

**Author:** ![craigb](https://avatars.discourse-cdn.com/v4/letter/c/9f8e36/32.png) [@craigb](https://forum.inductiveautomation.com/u/craigb)\
**Post date:** [September 27, 2022, 1:48pm UTC](https://forum.inductiveautomation.com/t/sql-runscript-in-expression-binding/64505/8 "2022-09-27T13:48:18Z")

</div>

Your right, I was looking in the wrong place. 🙃 I was looking at the runScalarQuery Docs

[https://docs.inductiveautomation.com/display/DOC81/system.db.runScalarQuery](https://docs.inductiveautomation.com/display/DOC81/system.db.runScalarQuery)
