# Event driven query tags null

**URL:** <https://forum.inductiveautomation.com/t/event-driven-query-tags-null/80441>\
**Category:** Ignition\
**Created:** [October 13, 2023, 12:49pm UTC](https://forum.inductiveautomation.com/t/event-driven-query-tags-null/80441 "2023-10-13T12:49:15Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![zacharyw.larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zacharyw.larson/32/23749_2.png) [@zacharyw.larson](https://forum.inductiveautomation.com/u/zacharyw.larson)\
**Post date:** [October 13, 2023, 12:49pm UTC](https://forum.inductiveautomation.com/t/event-driven-query-tags-null/80441/1 "2023-10-13T12:49:15Z")

</div>

I have two tags that were set to value source query that are returning 'null'.  
They are event driven.  
Ignition 8.1.19

When I run the queries, I get the right values.

How do I make it so that those never say null again, or if they say null they check their value again?

---

<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:** [October 13, 2023, 12:54pm UTC](https://forum.inductiveautomation.com/t/event-driven-query-tags-null/80441/2 "2023-10-13T12:54:37Z")

</div>

> Value Source: Query

> When I run the queries, ...

When you run them where?

**Checklist**

- Is the query returning a scaler value?
- Is your Data Type set correctly for the returned value datatype?

---

<div class="post-metadata">

**Author:** ![zacharyw.larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zacharyw.larson/32/23749_2.png) [@zacharyw.larson](https://forum.inductiveautomation.com/u/zacharyw.larson)\
**Post date:** [October 13, 2023, 1:05pm UTC](https://forum.inductiveautomation.com/t/event-driven-query-tags-null/80441/3 "2023-10-13T13:05:30Z")

</div>

Datatype is integer for both the tag and column in the table.  
The value returned is just the select top 1 myColumn from table where clause

Two things, the query had been working fine, never touched it in a long time.  
Suddenly yesterday, I hear that a value was showing wrong on a screen.  
I check today, value is null.

I run the query in the query browser, returns exactly the value I expect.  
I check the tags used in the query, their values are correct.

I changed a value of an a memory tag used in the driven query, still query is null.  
I set the tag to fixed rate, still value is null.  
I run in the query browser, still get the correct value.

A query using the same query, except selecting a different column, is returning the correct value.

* * *

I plugged in numbers instead of tag reads on the query, immediately right value shows.

I put the tags references back into the query, and reads null again.

Now I am really confused. Suddenly the value is reading correctly.  
I didn't even change anything.

But I need these values to always read correctly. I can't have this part of the time working. I am not sure how to make it robust.

* * *

I have three of these tags. They all use the same exact queries except different columns.

The one is reading the value correctly after I had removed the tag references, and put them back.  
(speed per hour)

The second uses exactly the same query, except different column. It doesn't work.  
(expected speed per hour)

The third is a string type, and never read wrong, still reads right.  
(production type name string based on the data collected from the same tags)

I can't share an image of it. I think I need to do maybe a support call.

---

<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:** [October 13, 2023, 1:25pm UTC](https://forum.inductiveautomation.com/t/event-driven-query-tags-null/80441/4 "2023-10-13T13:25:35Z")

</div>

> [@zacharyw.larson](#):
>
> I plugged in numbers instead of tag reads on the query, immediately right value shows.

Are you using another tag as a parameter in a tag query or what?  
Show your query syntax.

---

<div class="post-metadata">

**Author:** ![zacharyw.larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zacharyw.larson/32/23749_2.png) [@zacharyw.larson](https://forum.inductiveautomation.com/u/zacharyw.larson)\
**Post date:** [October 13, 2023, 1:31pm UTC](https://forum.inductiveautomation.com/t/event-driven-query-tags-null/80441/5 "2023-10-13T13:31:10Z")

</div>

```auto
select top 1 myFirstTagCorrectlyShowingNowInt from myTable
where thirdColumn = {[.]}aSpeedDataTag.value}
and fourthColum = case when thirdColumn =42 then {[.]aSecondSpeedDataTag.value} else 4 end
and production = 'B2'

```

```auto

select top 1 mySecondTagStillShowingNullInt from myTable
where thirdColumn = {[.]}aSpeedDataTag.value}
and fourthColum = case when thirdColumn =42 then {[.]aSecondSpeedDataTag.value} else 4 end
and production = 'B2'

```

```auto

select top 1 myThirdTagBeenShowingCorrecltyString from myTable
where thirdColumn = {[.]}aSpeedDataTag.value}
and fourthColum = case when thirdColumn =42 then {[.]aSecondSpeedDataTag.value} else 4 end
and production = 'B2'

```

The second and third query tags, I didn't change my tag references.  
If I change the second one's tag references, like remove.  
Apply  
Then if I put the tag references back, I think it will work.

I think I need a better way though, this should have been working the whole time.

---

<div class="post-metadata">

**Author:** ![zacharyw.larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zacharyw.larson/32/23749_2.png) [@zacharyw.larson](https://forum.inductiveautomation.com/u/zacharyw.larson)\
**Post date:** [October 13, 2023, 2:02pm UTC](https://forum.inductiveautomation.com/t/event-driven-query-tags-null/80441/6 "2023-10-13T14:02:08Z")

</div>

OMG

Someone, possibly me? added an 's' to the tag name.  
Or removed an s from the tag name in the query.

If it happened yesterday, it was definitely not me.

Thanks for your patience and help @Transistor

---

<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:** [October 13, 2023, 2:12pm UTC](https://forum.inductiveautomation.com/t/event-driven-query-tags-null/80441/7 "2023-10-13T14:12:13Z")

</div>

> [@zacharyw.larson](#):
>
> `fourthColum `

You might as well fix the spelling here while you're at it!

---

<div class="post-metadata">

**Author:** ![zacharyw.larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zacharyw.larson/32/23749_2.png) [@zacharyw.larson](https://forum.inductiveautomation.com/u/zacharyw.larson)\
**Post date:** [October 13, 2023, 2:32pm UTC](https://forum.inductiveautomation.com/t/event-driven-query-tags-null/80441/8 "2023-10-13T14:32:26Z")

</div>

The average person makes 7 mistakes an hour. I probably make 8 an hour.

---

<div class="post-metadata">

**Author:** ![Zach\_Larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zach_larson/32/54388_2.png) [@Zach\_Larson](https://forum.inductiveautomation.com/u/Zach_Larson)\
**Post date:** [April 1, 2024, 6:09pm UTC](https://forum.inductiveautomation.com/t/event-driven-query-tags-null/80441/9 "2024-04-01T18:09:36Z")

</div>

I am trying to do this sort of thing again.

The query tag value when set to sting type shows 1.

The query tag value set as a data type shows value as the column name.  
There is a 1 in the value for that column.

I get an error if I try to use 'as name' in the select.

```auto
select top 1 {[.]partString
from mytable t1
left join secondTable t2 on 
t1.line=t2.line
where part like concat('%','1b2345', '%')
and location ='B1'

```

I think the issue is that the string for partString starts with a number.

* * *

PTurmel saved me again.  
I needed to add quote marks.

> [@Query tag to alleviate some query workloads on perspective pages](https://forum.inductiveautomation.com/t/query-tag-to-alleviate-some-query-workloads-on-perspective-pages/59302/10):
>
> '{[~]Shifts/Current Shift Start}' \> '2022-01-25 07:00:00' But, still might not work because the date/time will be formatted using java's style. Consider not using a query tag. Use a memory tag, and use a gateway tag change event on shift start to run a PrepQuery. That can use ? substitution to avoid string conversion. Write the result dataset to the memory tag.

```auto
select top 1 '{[.]partString'
from mytable t1
left join secondTable t2 on 
t1.line=t2.line
where part like concat('%','1b2345', '%')
and location ='B1'

```

---

<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:** [April 1, 2024, 6:57pm UTC](https://forum.inductiveautomation.com/t/event-driven-query-tags-null/80441/10 "2024-04-01T18:57:57Z")

</div>

Query tags aren't safe to use with strings, as there's no way to parameterize them properly (yet). You should use a named query in a timer script that writes its result to a memory tag.

---

<div class="post-metadata">

**Author:** ![Zach\_Larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zach_larson/32/54388_2.png) [@Zach\_Larson](https://forum.inductiveautomation.com/u/Zach_Larson)\
**Post date:** [April 1, 2024, 7:04pm UTC](https://forum.inductiveautomation.com/t/event-driven-query-tags-null/80441/11 "2024-04-01T19:04:36Z")

</div>

You mean that if I do something like:

```auto
select top 1 partNumber
from mytable t1
left join secondTable t2 on 
t1.line=t2.line
where sku like concat('%','{[.]skuString', '%')
and ord like concat('%','{[.]ordString', '%')
and location ='B1'

```

Then I will miss some query runs because the tag will not detect that the tags in the where clause updated?

Or do you mean something worse than missing the updates?

---

<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:** [April 1, 2024, 7:11pm UTC](https://forum.inductiveautomation.com/t/event-driven-query-tags-null/80441/12 "2024-04-01T19:11:28Z")

</div>

By safe, I mean "non-hackable". If a malicious source puts certain special characters into your strings, including an unexpected closing quote, severe damage to your DB can result.

---

<div class="post-metadata">

**Author:** ![Zach\_Larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zach_larson/32/54388_2.png) [@Zach\_Larson](https://forum.inductiveautomation.com/u/Zach_Larson)\
**Post date:** [April 1, 2024, 7:15pm UTC](https://forum.inductiveautomation.com/t/event-driven-query-tags-null/80441/13 "2024-04-01T19:15:37Z")

</div>

Thanks

I was wondering if I could trust tags from the plc, but I probably can't trust the strings.

* * *

In my case, I will need to write an event script on my order tag and run a named query from a change event script.  
Then I will write that.  
I get the current order from my plc.
