# How retrieve data from a table using runNamedQuery

**URL:** <https://forum.inductiveautomation.com/t/how-retrieve-data-from-a-table-using-runnamedquery/74980>\
**Category:** Ignition\
**Tags:** ignition81, perspective\
**Created:** [June 13, 2023, 7:59pm UTC](https://forum.inductiveautomation.com/t/how-retrieve-data-from-a-table-using-runnamedquery/74980 "2023-06-13T19:59:45Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![mjohnson0238](https://avatars.discourse-cdn.com/v4/letter/m/4bbf92/32.png) [@mjohnson0238](https://forum.inductiveautomation.com/u/mjohnson0238)\
**Post date:** [June 13, 2023, 7:59pm UTC](https://forum.inductiveautomation.com/t/how-retrieve-data-from-a-table-using-runnamedquery/74980/1 "2023-06-13T19:59:45Z")

</div>

Good day all. I am trying to retrieve data from table using:

```auto
namedQuery = system.db.runNamedQuery('WasteBarrelDesc')

```

This table contains a TID column and a BarrelNum column. I want the know the BarrelNum based on the TID.

How can I achieve this?

---

<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:** [June 13, 2023, 8:01pm UTC](https://forum.inductiveautomation.com/t/how-retrieve-data-from-a-table-using-runnamedquery/74980/2 "2023-06-13T20:01:55Z")

</div>

Show us your Named Query. Post it as text.

* * *

On re-reading it appears that you are trying to create a named query in a script. That's not how it works. You create the named query in the Project Browser section labelled _Named Query_. You then use it in your script in the manner of,  
`results = system.db.runNamedQuery('WasteBarrelDesc')`

[https://docs.inductiveautomation.com/display/DOC81/Named+Queries](https://docs.inductiveautomation.com/display/DOC81/Named+Queries)

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

---

<div class="post-metadata">

**Author:** ![mjohnson0238](https://avatars.discourse-cdn.com/v4/letter/m/4bbf92/32.png) [@mjohnson0238](https://forum.inductiveautomation.com/u/mjohnson0238)\
**Post date:** [June 14, 2023, 12:51am UTC](https://forum.inductiveautomation.com/t/how-retrieve-data-from-a-table-using-runnamedquery/74980/3 "2023-06-14T00:51:52Z")

</div>

my named query is this:

```auto
Select *
From WasteBarrelDesc

```

right now the contents are just the one row. It will be several more after this trail.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/a/9/a9f48df078a84b993e51ed115346c2f53ff01a7f.png)

Im wanting grab the BarrelNum, in reference to the TID.

---

<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:** [June 14, 2023, 6:20am UTC](https://forum.inductiveautomation.com/t/how-retrieve-data-from-a-table-using-runnamedquery/74980/4 "2023-06-14T06:20:15Z")

</div>

```auto
SELECT BarrelType
FROM WasteBarrelDesc
WHERE TID = :tidNum

```

`:tidNum` will be a parameter of the named query.

It's all explained in the Named Queries document link above and there's probably a video on Inductive University.

The manual's runNamedQuery page shows how to pass the parameter in the `system.db.runNamedQuery()` function.

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [June 14, 2023, 7:07am UTC](https://forum.inductiveautomation.com/t/how-retrieve-data-from-a-table-using-runnamedquery/74980/5 "2023-06-14T07:07:34Z")

</div>

If you're binding a perspective table to a query, you probably don't need to use `runNamedQuery`. Just use a query binding.  
Don't use scripting unless necessary. Ignition has a lot a built-in ways of doing things efficiently, use them before resorting to custom methods.

---

<div class="post-metadata">

**Author:** ![mjohnson0238](https://avatars.discourse-cdn.com/v4/letter/m/4bbf92/32.png) [@mjohnson0238](https://forum.inductiveautomation.com/u/mjohnson0238)\
**Post date:** [June 16, 2023, 12:12pm UTC](https://forum.inductiveautomation.com/t/how-retrieve-data-from-a-table-using-runnamedquery/74980/6 "2023-06-16T12:12:25Z")

</div>

Sorry for the late reply, Ive been out of town.

I am using the namedQuery in python. I have come up with something like this:

```auto
# START Getting Barrel Number based on TID
	barrelNum_namedQuery = system.db.runNamedQuery('WasteBarrelDesc') 
	dataset = barrelNum_namedQuery
	for i in range(dataset.getRowCount()):
	    if dataset.getValueAt(i, 'TID') == TID:
	        barrelNum = dataset.getValueAt(i, 'BarrelNum')
	        break
	else:
	    print("No matching row found.")
	# END Getting Barrel Number based on TID

```

I am making it execute via an "on change" script of a tag. Is this an OK way of doing it?

---

<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:** [June 16, 2023, 12:20pm UTC](https://forum.inductiveautomation.com/t/how-retrieve-data-from-a-table-using-runnamedquery/74980/7 "2023-06-16T12:20:00Z")

</div>

> [@mjohnson0238](#):
>
> via an "on change" script of a tag

That is a dangerous place to run database operations. See this and its linked discussions:

> [@Tag value change event](https://forum.inductiveautomation.com/t/tag-value-change-event/74998/4):
>
> This. Events on tags themselves should run in single digit milliseconds or less, due to the likelihood of blocking the system-wide thread pool for tags. That means no database operations nor any network access operations nor any recursive searches in these events. Gateway tag change events in a project have their own (unbounded) queues and their own threads. Put any time-consuming operations there. But do note that only one thread services all the tags for a given event, so there can be mo…

If you must, you will need to change the call syntax to include the NQ project name to comply with that function's gateway scope requirement.

---

<div class="post-metadata">

**Author:** ![mjohnson0238](https://avatars.discourse-cdn.com/v4/letter/m/4bbf92/32.png) [@mjohnson0238](https://forum.inductiveautomation.com/u/mjohnson0238)\
**Post date:** [June 16, 2023, 12:45pm UTC](https://forum.inductiveautomation.com/t/how-retrieve-data-from-a-table-using-runnamedquery/74980/8 "2023-06-16T12:45:28Z")

</div>

> [@pturmel](#):
>
> If you must, you will need to change the call syntax to include the NQ project name to comply with that function's gateway scope requirement.

Can you show me an example, Im not sure what you mean on this. Thank you

---

<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:** [June 16, 2023, 1:01pm UTC](https://forum.inductiveautomation.com/t/how-retrieve-data-from-a-table-using-runnamedquery/74980/9 "2023-06-16T13:01:50Z")

</div>

> [@mjohnson0238](#):
>
> Im not sure what you mean

Every system function has documentation describing the differences in usage in various calling scopes, if any.

Let me repeat an earlier link:

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

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [June 16, 2023, 2:07pm UTC](https://forum.inductiveautomation.com/t/how-retrieve-data-from-a-table-using-runnamedquery/74980/10 "2023-06-16T14:07:28Z")

</div>

That's really not how you want to be doing this.

Where do you want to display that barrel num ?  
I'm going to suppose it's on a label, in a view.

Create a new named query.  
Set it to be a SCALAR query.  
Add a parameter of type string, call it `TID`.  
Write the query so it looks like this

```sql
SELECT BarrelNum
FROM your_barrel_table
WHERE TID = :TID

```

In the view where you want to display it, put a label.  
Put a query binding on that label, and use your new named query.  
Pass the `TID` to look for as the parameter to the query.

If you REALLY want to use your already existing named query (but don't), add a custom property on the label, put a query binding on that property, use your named query.  
Now put a structure binding on the label, and add the custom property and the TID source.  
Add an expression transform and use [lookup](https://docs.inductiveautomation.com/display/DOC81/lookup) to retrieve your barrel num.

No scripting needed anywhere, no change scripts, no touching tags.
