# Can I use a query within a Tag event script?

**URL:** <https://forum.inductiveautomation.com/t/can-i-use-a-query-within-a-tag-event-script/54197>\
**Category:** Ignition\
**Tags:** database, vision, scripting\
**Created:** [December 16, 2021, 2:45pm UTC](https://forum.inductiveautomation.com/t/can-i-use-a-query-within-a-tag-event-script/54197 "2021-12-16T14:45:40Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Connor\_Moore](https://avatars.discourse-cdn.com/v4/letter/c/a87d85/32.png) [@Connor\_Moore](https://forum.inductiveautomation.com/u/Connor_Moore)\
**Post date:** [December 16, 2021, 2:45pm UTC](https://forum.inductiveautomation.com/t/can-i-use-a-query-within-a-tag-event-script/54197/1 "2021-12-16T14:45:40Z")

</div>

I have a measuring tool that pushes measurements to tags in my system and I need to check them against a database. What is the syntax to put my query in this Tag event script? The query has a job name that will be sent to the system and then the upper and lower bounds (the 1.98 and 1.19 values) should change based on the job title. Is there a way to do this in ignition.  
Here is my Tag Event Script  
If this prompt returns 0 it is out of bounds, if it returns 2 it is within regulation.  
radii = system.tag.read("[.]Radii")  
width = system.tag.read("[.]Width")  
if currentValue.value \>= 1.98 or currentValue.value \<= 1.19:  
system.tag.write("[.]Proxy",0)  
elif width.value \>= 1.98 or width.value \<= 1.19:  
system.tag.write("[.]Proxy",0)  
elif radii.value \>= 1.98 or radii.value \<= 1.19:  
system.tag.write("[.]Proxy",0)  
else: system.tag.write("[.]Proxy",2)

---

<div class="post-metadata">

**Author:** ![amarks](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/amarks/32/45610_2.png) [@amarks](https://forum.inductiveautomation.com/u/amarks)\
**Post date:** [December 16, 2021, 3:14pm UTC](https://forum.inductiveautomation.com/t/can-i-use-a-query-within-a-tag-event-script/54197/2 "2021-12-16T15:14:18Z")

</div>

Create a named query and use system.db.runNamedQuery()

---

<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:** [December 16, 2021, 3:34pm UTC](https://forum.inductiveautomation.com/t/can-i-use-a-query-within-a-tag-event-script/54197/3 "2021-12-16T15:34:30Z")

</div>

You can, but note that any delays or heavy traffic can bog down your whole tag system. I would use a query tag to hold the current applicable bounds and reference that in the actual value script.

---

<div class="post-metadata">

**Author:** ![Connor\_Moore](https://avatars.discourse-cdn.com/v4/letter/c/a87d85/32.png) [@Connor\_Moore](https://forum.inductiveautomation.com/u/Connor_Moore)\
**Post date:** [December 16, 2021, 4:02pm UTC](https://forum.inductiveautomation.com/t/can-i-use-a-query-within-a-tag-event-script/54197/4 "2021-12-16T16:02:12Z")

</div>

> [@pturmel](#):
>
> ould use a query tag to hold the current applicable bounds and reference that in the actual value script

Within the query tag is it possible to select only the data connected to my job name tag? Im having trouble with the proper syntax. Something like

SELECT heightmin FROM rec\_profiles  
WHERE name = tag name value

---

<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:** [December 16, 2021, 4:19pm UTC](https://forum.inductiveautomation.com/t/can-i-use-a-query-within-a-tag-event-script/54197/5 "2021-12-16T16:19:45Z")

</div>

I would pull all of the currently needed measurement limits into a single query tag with a column for the tag names that they correspond to. Then your tag value script picks the correct row and column from that dataset.

---

<div class="post-metadata">

**Author:** ![Connor\_Moore](https://avatars.discourse-cdn.com/v4/letter/c/a87d85/32.png) [@Connor\_Moore](https://forum.inductiveautomation.com/u/Connor_Moore)\
**Post date:** [December 16, 2021, 5:53pm UTC](https://forum.inductiveautomation.com/t/can-i-use-a-query-within-a-tag-event-script/54197/6 "2021-12-16T17:53:10Z")

</div>

> [@Connor\_Moore](#):
>
> job = system.tag.read("[.]Job")  
> if currentValue.value \>= NEED HEIGHTMAX FROM QUERY or currentValue.value \<= NEED HEIGHTMIN FROM QUERY:  
> system.tag.write("[.]Proxy",0)

sorry to bother again but how do I bring the correct query row into my tag value script? Do I use SQL syntax within this statement?

---

<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:** [December 16, 2021, 6:15pm UTC](https://forum.inductiveautomation.com/t/can-i-use-a-query-within-a-tag-event-script/54197/7 "2021-12-16T18:15:17Z")

</div>

Please show your “rec\_profiles” profiles table structure. And describe how you need to use it and for how many tags. That will make specific advice possible.

It is also possible that an expression tag is more appropriate than a script. But let’s see everything involved.

---

<div class="post-metadata">

**Author:** ![jespinmartin1](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jespinmartin1/32/57175_2.png) [@jespinmartin1](https://forum.inductiveautomation.com/u/jespinmartin1)\
**Post date:** [December 16, 2021, 7:10pm UTC](https://forum.inductiveautomation.com/t/can-i-use-a-query-within-a-tag-event-script/54197/8 "2021-12-16T19:10:57Z")

</div>

I would do it with 2 queries.

_ForMax_

```auto
SELECT MAX(height) FROM rec_profiles WHERE name = tag name value

```

_ForMin_

```auto
SELECT MIN(height) FROM rec_profiles WHERE name = tag name value

```

Or maybe in a single query which hopefully is small by x Rows and 1 Col dataset, you can try

```auto
data = datasetQuery.getData()
min = min(i for i in data[0])
max = max(i for i in data[0])

```

---

<div class="post-metadata">

**Author:** ![Connor\_Moore](https://avatars.discourse-cdn.com/v4/letter/c/a87d85/32.png) [@Connor\_Moore](https://forum.inductiveautomation.com/u/Connor_Moore)\
**Post date:** [December 16, 2021, 7:44pm UTC](https://forum.inductiveautomation.com/t/can-i-use-a-query-within-a-tag-event-script/54197/9 "2021-12-16T19:44:43Z")

</div>

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/3/e/3e83aefb5ecadedb39d5ee20a1a9e738aa16fcb5.png)  
Here is the table.  
There are 31 different jobs each with their own min and maxes.  
The job name will be reported to the tag “name”

This is the script on the height tag  
radii = system.tag.read("[.]Radii")  
width = system.tag.read("[.]Width")  
if currentValue.value \>= HEIGHTMAX FROM QUERY or currentValue.value \<= HEIGHTMIN FOR QUERY:  
system.tag.write("[.]Proxy",0)  
elif width.value \>= WIDTHMAX FROM QUERY or width.value \<= WIDTHMIN FROM QUERY:  
system.tag.write("[.]Proxy",0)  
elif radii.value \>= CORNERMAX FROM QUERY or radii.value \<= CORNERMIN FROM QUERY:  
system.tag.write("[.]Proxy",0)  
else: system.tag.write("[.]Proxy",2)

Proxy is the tag in which this is writing too. 0 is not within, 2 if within

---

<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:** [December 16, 2021, 7:54pm UTC](https://forum.inductiveautomation.com/t/can-i-use-a-query-within-a-tag-event-script/54197/10 "2021-12-16T19:54:42Z")

</div>

So, I’d create a query tag(with named query), named `RecProfile` perhaps, that executes in event driven mode that simply pulls the entire row that matches the current job. Something like this:

```auto
SELECT * FROM rec_profiles WHERE name = :job

```

In the tag setup, supply `{[.]job}` for the `:job` query parameter.

Then, I’d change the `Proxy` tag to be an expression, with this:

```auto
if({[.]Height} >= {[.]RecProfile}[0, 'heightmin'] &&
	{[.]Height} <= {[.]RecProfile}[0, 'heightmax'] &&
	{[.]Width} >= {[.]RecProfile}[0, 'widthmin'] &&
	{[.]Width} <= {[.]RecProfile}[0, 'widthmax'] &&
	{[.]Radii} >= {[.]RecProfile}[0, 'cornermin'] &&
	{[.]Radii} <= {[.]RecProfile}[0, 'cornermax'],
	2, 0)

```

No script.

---

<div class="post-metadata">

**Author:** ![Connor\_Moore](https://avatars.discourse-cdn.com/v4/letter/c/a87d85/32.png) [@Connor\_Moore](https://forum.inductiveautomation.com/u/Connor_Moore)\
**Post date:** [December 16, 2021, 8:11pm UTC](https://forum.inductiveautomation.com/t/can-i-use-a-query-within-a-tag-event-script/54197/11 "2021-12-16T20:11:31Z")

</div>

Thank you so much, that worked perfectly!

---

<div class="post-metadata">

**Author:** ![Connor\_Moore](https://avatars.discourse-cdn.com/v4/letter/c/a87d85/32.png) [@Connor\_Moore](https://forum.inductiveautomation.com/u/Connor_Moore)\
**Post date:** [December 22, 2021, 5:18pm UTC](https://forum.inductiveautomation.com/t/can-i-use-a-query-within-a-tag-event-script/54197/12 "2021-12-22T17:18:12Z")

</div>

Your proxy expression worked perfectly but I need to modify it to accommodate a close to being bad. I am not the best with python and I understand the logic behind && but I do not understand the [0, ‘heightmin’] part. I assume the heightmin is telling the {[.]RecProfile} which part of the query to address. Could you help me on this?

The close to bad will be  
Height \>= min \<= (HeightMax - (.1 \* (Max - Min))  
close to good will be  
Height \<= max \>= (HeightMin + (.1 \* (Max - Min))

---

<div class="post-metadata">

**Author:** ![Connor\_Moore](https://avatars.discourse-cdn.com/v4/letter/c/a87d85/32.png) [@Connor\_Moore](https://forum.inductiveautomation.com/u/Connor_Moore)\
**Post date:** [December 22, 2021, 5:21pm UTC](https://forum.inductiveautomation.com/t/can-i-use-a-query-within-a-tag-event-script/54197/13 "2021-12-22T17:21:18Z")

</div>

Output should be  
2 for good  
1 for close  
0 for bad

---

<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:** [December 22, 2021, 5:23pm UTC](https://forum.inductiveautomation.com/t/can-i-use-a-query-within-a-tag-event-script/54197/14 "2021-12-22T17:23:15Z")

</div>

So, this isn’t python. It is Ignition’s expression language. The square brackets after a dataset reference access the content of that dataset, by row and column. Described here:

[https://docs.inductiveautomation.com/display/DOC81/Expression+Language+and+Syntax#ExpressionLanguageandSyntax-DatasetAccess](https://docs.inductiveautomation.com/display/DOC81/Expression+Language+and+Syntax#ExpressionLanguageandSyntax-DatasetAccess)

Simply take the {dataset}[subscripts] construct and substitute into your updated equations.

---

<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:** [December 22, 2021, 5:27pm UTC](https://forum.inductiveautomation.com/t/can-i-use-a-query-within-a-tag-event-script/54197/15 "2021-12-22T17:27:09Z")

</div>

I would recommend altering your “RecProfile” dataset query to supply the “warning” boundaries so they don’t have to be recomputed for every comparison.

---

<div class="post-metadata">

**Author:** ![Connor\_Moore](https://avatars.discourse-cdn.com/v4/letter/c/a87d85/32.png) [@Connor\_Moore](https://forum.inductiveautomation.com/u/Connor_Moore)\
**Post date:** [December 22, 2021, 8:59pm UTC](https://forum.inductiveautomation.com/t/can-i-use-a-query-within-a-tag-event-script/54197/16 "2021-12-22T20:59:22Z")

</div>

Here is the expression I ended with after adding the warning labels for those of who want to see.  
if({[.]Diameter} \>= {[.]CirProfile}[0, ‘warningmin’] &&  
{[.]Diameter} \<= {[.]CirProfile}[0, ‘warningmax’],  
2,if({[.]Diameter} \>= {[.]CirProfile}[0, ‘diametermin’] &&  
{[.]Diameter} \<= {[.]CirProfile}[0, ‘diametermax’]  
,1,0))
