# runNamedQuery()

**URL:** <https://forum.inductiveautomation.com/t/runnamedquery/68073>\
**Category:** Ignition\
**Created:** [December 29, 2022, 5:52pm UTC](https://forum.inductiveautomation.com/t/runnamedquery/68073 "2022-12-29T17:52:24Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tony\_Leah](https://avatars.discourse-cdn.com/v4/letter/t/f6c823/32.png) [@Tony\_Leah](https://forum.inductiveautomation.com/u/Tony_Leah)\
**Post date:** [December 29, 2022, 5:52pm UTC](https://forum.inductiveautomation.com/t/runnamedquery/68073/1 "2022-12-29T17:52:24Z")

</div>

I am trying to run "runNamedQuery()" from a script. I want to use it like I would the following:  
data = system.db.runQuery(SELECT \* FROM datatable)  
Then I would use data[0][1] or something similar throughout my script.  
When I try using  
data = system.db.runNamedQuery(queryName, params)  
I never have any data in the data dataset.

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [December 29, 2022, 6:34pm UTC](https://forum.inductiveautomation.com/t/runnamedquery/68073/2 "2022-12-29T18:34:54Z")

</div>

1. Does your named query return data in the Named Query Testing tab? (You didn't say.)
2. Does your named query return data when run from the Script Console?
3. Your statement,  
`data = system.db.runQuery(SELECT * FROM datatable)`  
should be quoted thus:  
`data = system.db.runQuery("SELECT * FROM datatable")`  
but named queries are to be preferred.

Tip: use the `</>` code formatting button to preserve code indentation and apply syntax highlighting.

---

<div class="post-metadata">

**Author:** ![Tony\_Leah](https://avatars.discourse-cdn.com/v4/letter/t/f6c823/32.png) [@Tony\_Leah](https://forum.inductiveautomation.com/u/Tony_Leah)\
**Post date:** [December 29, 2022, 8:00pm UTC](https://forum.inductiveautomation.com/t/runnamedquery/68073/3 "2022-12-29T20:00:39Z")

</div>

The named query does work in the testing tab.

The named query does not return data from the script console

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [December 29, 2022, 8:16pm UTC](https://forum.inductiveautomation.com/t/runnamedquery/68073/4 "2022-12-29T20:16:07Z")

</div>

> [@Tony\_Leah](#):
>
> The named query does not return data from the script console.

Then please show how you are setting up the `params` variable.

---

<div class="post-metadata">

**Author:** ![Tony\_Leah](https://avatars.discourse-cdn.com/v4/letter/t/f6c823/32.png) [@Tony\_Leah](https://forum.inductiveautomation.com/u/Tony_Leah)\
**Post date:** [December 30, 2022, 2:32am UTC](https://forum.inductiveautomation.com/t/runnamedquery/68073/5 "2022-12-30T02:32:14Z")

</div>

When you run the following script:

# Create a python dictionary of parameters to pass

params ` `=` `{` `"param1"` `:` `"my string"` `, ` `"param2"` `:` `10` `}

# Run the Named Query

system.db.runNamedQuery(` `"myUpdateQuery"` `, params)

Or when you run this script:

# Request the Named Query with an empty dictionary as the second parameter.

system.db.runNamedQuery(` `"folderName/myNamedQuery"` `, {})

How do you get the results of the query?

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [December 30, 2022, 2:47am UTC](https://forum.inductiveautomation.com/t/runnamedquery/68073/6 "2022-12-30T02:47:11Z")

</div>

Please edit your post and format your code blocks with the \</\> button.

---

<div class="post-metadata">

**Author:** ![Tony\_Leah](https://avatars.discourse-cdn.com/v4/letter/t/f6c823/32.png) [@Tony\_Leah](https://forum.inductiveautomation.com/u/Tony_Leah)\
**Post date:** [December 30, 2022, 3:24am UTC](https://forum.inductiveautomation.com/t/runnamedquery/68073/7 "2022-12-30T03:24:08Z")

</div>

I figured out how to pull the data out of the runNamedQuery script. i.e. how to get the results from running the query.

---

<div class="post-metadata">

**Author:** ![Miguel\_Gomez](https://avatars.discourse-cdn.com/v4/letter/m/f0a364/32.png) [@Miguel\_Gomez](https://forum.inductiveautomation.com/u/Miguel_Gomez)\
**Post date:** [February 16, 2024, 5:48pm UTC](https://forum.inductiveautomation.com/t/runnamedquery/68073/8 "2024-02-16T17:48:26Z")

</div>

And how did you do that? I have the same problem right now.

Thank you

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [February 16, 2024, 5:52pm UTC](https://forum.inductiveautomation.com/t/runnamedquery/68073/9 "2024-02-16T17:52:03Z")

</div>

Please show a preformatted text snip of your code. The context of the OP and follow-ups here is extremely vague as to what the actual problem was.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [February 16, 2024, 5:53pm UTC](https://forum.inductiveautomation.com/t/runnamedquery/68073/10 "2024-02-16T17:53:22Z")

</div>

It is a function. You assign the return value to a variable. Something like this:

```auto
ds = system.db.runNamedQuery(....)

```

(This is pretty fundamental python behavior. Have you taken any training?)

---

<div class="post-metadata">

**Author:** ![Miguel\_Gomez](https://avatars.discourse-cdn.com/v4/letter/m/f0a364/32.png) [@Miguel\_Gomez](https://forum.inductiveautomation.com/u/Miguel_Gomez)\
**Post date:** [February 16, 2024, 6:26pm UTC](https://forum.inductiveautomation.com/t/runnamedquery/68073/11 "2024-02-16T18:26:26Z")

</div>

I have a Named Query called _Select Report Number B_

```auto
SELECT id_test_report FROM _test_report 
ORDER BY id_test_report DESC LIMIT 1;

```

This Query gives me the last number report and I need to send that number to another table. That is not the problem, the problem is that I had tried to get that value in my script like this:

```auto
	report=system.db.runNamedQuery("Select Report Number B")
	reportValue=report[0]

```

or

```auto
	system.db.runNamedQuery("Select Report Number B", paramReport)
	reportValue=paramReport[0]

```

Sorry if these kind of very easy questions of pretty fundamental python make the eyes of the Gods hurt but the ignition page is down  
[https://www.docs.inductiveautomation.com/display/DOC81/system.db.runNamedQuery](https://www.docs.inductiveautomation.com/display/DOC81/system.db.runNamedQuery)

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [February 16, 2024, 6:33pm UTC](https://forum.inductiveautomation.com/t/runnamedquery/68073/12 "2024-02-16T18:33:38Z")

</div>

> [@Miguel\_Gomez](#):
>
> Sorry if these kind of very easy questions make the eyes of the Gods hurt

No need to apologize, it's much easier to help with proper details. Thanks for those.

Make sure you have configured the Named Query as scalar. Then what you need is:

```auto
reportValue = system.db.runNamedQuery("Select Report Number B", {})

```

> [@Miguel\_Gomez](#):
>
> the ignition page is down

No, its just in a different place now.

> **[system.db.runNamedQuery | Ignition User Manual](https://www.docs.inductiveautomation.com/docs/8.1/appendix/scripting-functions/system-db/system-db-runNamedQuery)**
>
> Runs a named query and returns the results.

See this post:

> [@Ignition User Manual updates](https://forum.inductiveautomation.com/t/ignition-user-manual-updates/85448):
>
> We’re launching a new look and feel for the Ignition User Manual next week, on February 15, 2024. This is a project that the IA Docs team has been working hard on for about a year now, and we’re looking forward to hearing your feedback! Here are a few of the features we’re most excited about: Better signposts to indicate the current version A more responsive layout that will adapt for mobile devices [2024-02-08\_10-38-28] Common resources more prominently featured in th…

---

<div class="post-metadata">

**Author:** ![Miguel\_Gomez](https://avatars.discourse-cdn.com/v4/letter/m/f0a364/32.png) [@Miguel\_Gomez](https://forum.inductiveautomation.com/u/Miguel_Gomez)\
**Post date:** [February 16, 2024, 6:40pm UTC](https://forum.inductiveautomation.com/t/runnamedquery/68073/13 "2024-02-16T18:40:03Z")

</div>

Thank you!,

Now is working, I really appreciate all your help and thank you for the documentation update!

---

<div class="post-metadata">

**Author:** ![jacque\_trahan](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jacque_trahan/32/58886_2.png) [@jacque\_trahan](https://forum.inductiveautomation.com/u/jacque_trahan)\
**Post date:** [February 19, 2024, 10:08pm UTC](https://forum.inductiveautomation.com/t/runnamedquery/68073/14 "2024-02-19T22:08:36Z")

</div>

One more aspect to be aware of/may save you some time in the future. This got me good for a while on fully understanding the different scopes.

I was using the runNamedQuery() in the alarm pipeline and I forgot that is a Gateway scope module. So the name of my project needed to be added. Took me way longer to figure out than it should have.

example:  
Script console ([designer scope?](https://www.docs.inductiveautomation.com/docs/8.1/platform/designer/designer-tools/script-console) only works in designer):

```auto
db = system.db.runNamedQuery("Your Saved Name Query", {"pram1":"value",etc})

```

vs the alarm pipeline (Gateway scope)

```auto
db = system.db.runNamedQuery("ProjectName", "Your Saved Name Query", {"pram1":"value",etc})

```

If needed this post is really good at [knowing what scope](https://forum.inductiveautomation.com/t/can-i-tell-what-scope-a-script-is-running-from/70880) a script is coming from

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [February 20, 2024, 12:37am UTC](https://forum.inductiveautomation.com/t/runnamedquery/68073/15 "2024-02-20T00:37:24Z")

</div>

> [@jacque\_trahan](#):
>
> designer scope?

Designer Scope is a variant of Vision Client Scope. FYI.
