# Refresh or update a query tag

**URL:** <https://forum.inductiveautomation.com/t/refresh-or-update-a-query-tag/78361>\
**Category:** Ignition\
**Created:** [August 24, 2023, 9:25pm UTC](https://forum.inductiveautomation.com/t/refresh-or-update-a-query-tag/78361 "2023-08-24T21:25:40Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Omar\_Mohamed1](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@Omar\_Mohamed1](https://forum.inductiveautomation.com/u/Omar_Mohamed1)\
**Post date:** [August 24, 2023, 9:25pm UTC](https://forum.inductiveautomation.com/t/refresh-or-update-a-query-tag/78361/1 "2023-08-24T21:25:40Z")

</div>

I am new to ignition, I have been trying to look online for a solution to a problem I am having at the moment. I have a reference tag that takes the value of another OPC tag whenever it changes, I also have a query tag (dataset) and I want to trigger the Query tag to update or refresh whenever that reference tag changes its value. I tried to use this script to read the query tag dataset and write it to a new memory tag but it doesn't work

```
tags = system.tag.readBlocking("path_to_querytag")  

# Update the query tag value with the query result
system.tag.write("new_memory_tag_path", tags)

```

---

<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:** [August 24, 2023, 9:41pm UTC](https://forum.inductiveautomation.com/t/refresh-or-update-a-query-tag/78361/2 "2023-08-24T21:41:43Z")

</div>

There's no external way to refresh a query tag other than its natural schedule, short of restarting it. (Reading a tag just gets the latest value--it has nothing to do with execution.) If the query tag is on a tag group or set to periodic, it will rerun on schedule. If set to event mode, it will rerun when some reference it is using changes. Does the reference tag participate in the query tag's SQL?

Consider not using a query tag, but dataset memory tag. Run your query via scripting when necessary, and write the result to the memory tag.

---

<div class="post-metadata">

**Author:** ![Omar\_Mohamed1](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@Omar\_Mohamed1](https://forum.inductiveautomation.com/u/Omar_Mohamed1)\
**Post date:** [August 25, 2023, 1:21pm UTC](https://forum.inductiveautomation.com/t/refresh-or-update-a-query-tag/78361/3 "2023-08-25T13:21:50Z")

</div>

I tried that as well, but it gives me an error. I created a dataset memory tag (Writeable\_M) and I run the script in the reference tag value changed .  
That's my script

```auto
   query = "SELECT TOP 10 * FROM test"
	result = system.db.runQuery(query,"R21")

	system.tag.writeBlocking("[.]Writeable_M", result)

```

I get that error

```auto
Error executing script.
Traceback (most recent call last):
  File "<tagevent:valueChanged>", line 4, in valueChanged
	at com.inductiveautomation.ignition.common.script.builtin.AbstractDBUtilities.error(AbstractDBUtilities.java:392)
	at com.inductiveautomation.ignition.common.script.builtin.AbstractDBUtilities.runQuery(AbstractDBUtilities.java:359)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.base/java.lang.reflect.Method.invoke(Unknown Source)
java.lang.Exception: java.lang.Exception: Error executing system.db.runQuery(SELECT TOP 10 * FROM test, R21, )

```

---

<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:** [August 25, 2023, 1:30pm UTC](https://forum.inductiveautomation.com/t/refresh-or-update-a-query-tag/78361/4 "2023-08-25T13:30:12Z")

</div>

Two issues:

- When running a tag event, the script is outside any normal project scope, and therefore has no default database. The optional parameter to `system.db.runQuery` for the database is **not optional** in this situation.

- Running any kind of database query, unless it **always** executes extremely quickly (single-digit milliseconds), is inappropriate in tag event scripts. Use a gateway tag change script instead (in the project, subscribed to the reference tag, not **on** the reference tag).

{ Failure to follow the fast-execution rule for tag events can crush your entire system. }

---

<div class="post-metadata">

**Author:** ![Omar\_Mohamed1](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@Omar\_Mohamed1](https://forum.inductiveautomation.com/u/Omar_Mohamed1)\
**Post date:** [August 25, 2023, 3:24pm UTC](https://forum.inductiveautomation.com/t/refresh-or-update-a-query-tag/78361/5 "2023-08-25T15:24:05Z")

</div>

I added the following query in the gateway event script

```auto
# Update the query tag value with the query result
query = "SELECT TOP 10 * FROM test"
result = system.db.runQuery(query)

# Update the query tag value with the query result
system.tag.writeBlocking("[default]Writeable_M", result)

```

but when I change the value of the reference tag I added in the (tag paths) in the gateway tag change script, nothing changes in the memory dataset tag and the timestamp also doesn't change.  
I also tried the gateway event script with that code

```auto
system.tag.writeBlocking("[default]Writeable_N", 5)

```

to edit the value of an integer memory tag and it didn't write the value to that tag when the reference tag changed.

---

<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:** [August 25, 2023, 3:31pm UTC](https://forum.inductiveautomation.com/t/refresh-or-update-a-query-tag/78361/6 "2023-08-25T15:31:33Z")

</div>

Put a logger at the beginning of your gateway event script:

```auto
logger = system.util.getLogger("SomeLoggerName")
logger.info("I'm running")

```

Look for that in your gateway log.  
Make sure to save your project. Gateway project events don't run from designer.

---

<div class="post-metadata">

**Author:** ![Omar\_Mohamed1](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@Omar\_Mohamed1](https://forum.inductiveautomation.com/u/Omar_Mohamed1)\
**Post date:** [August 25, 2023, 3:52pm UTC](https://forum.inductiveautomation.com/t/refresh-or-update-a-query-tag/78361/7 "2023-08-25T15:52:14Z")

</div>

This is the error I found in the gateway scripts

```auto
com.inductiveautomation.ignition.common.script.JythonExecException: Traceback (most recent call last): File "", line 8, in ValueError: Length of values does not match length of paths.

at org.python.core.Py.ValueError(Py.java:334)

at com.inductiveautomation.ignition.common.script.builtin.AbstractTagUtilities.writeBlocking(AbstractTagUtilities.java:542)

at jdk.internal.reflect.GeneratedMethodAccessor141.invoke(Unknown Source)

at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

at java.base/java.lang.reflect.Method.invoke(Unknown Source)

at org.python.core.PyReflectedFunction. __call__ (PyReflectedFunction.java:190)

at com.inductiveautomation.ignition.common.script.ScriptManager$ReflectedInstanceFunction. __call__ (ScriptManager.java:552)

at org.python.core.PyObject. __call__ (PyObject.java:477)

at org.python.core.PyObject. __call__ (PyObject.java:481)

at org.python.pycode._pyx36.f$0(:8)

at org.python.pycode._pyx36.call_function()

at org.python.core.PyTableCode.call(PyTableCode.java:173)

at org.python.core.PyCode.call(PyCode.java:18)

at org.python.core.Py.runCode(Py.java:1687)

at com.inductiveautomation.ignition.common.script.ScriptManager.runCode(ScriptManager.java:803)

at com.inductiveautomation.ignition.gateway.project.ProjectScriptLifecycle$TrackingProjectScriptManager.runCode(ProjectScriptLifecycle.java:823)

at com.inductiveautomation.ignition.common.script.ScriptManager.runCode(ScriptManager.java:751)

at com.inductiveautomation.ignition.gateway.project.ProjectScriptLifecycle$TrackingProjectScriptManager.runCode(ProjectScriptLifecycle.java:804)

at com.inductiveautomation.ignition.common.script.TagChangeScriptExecutor$TagChangeExecutionCallback.execute(TagChangeScriptExecutor.java:242)

at com.inductiveautomation.ignition.common.script.TagChangeScriptExecutor$TagChangeExecutionCallback.execute(TagChangeScriptExecutor.java:194)

at com.inductiveautomation.ignition.common.util.SerialExecutionQueue$PollAndExecute.run(SerialExecutionQueue.java:102)

at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)

at java.base/java.util.concurrent.FutureTask.run(Unknown Source)

at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)

at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)

at java.base/java.lang.Thread.run(Unknown Source)

Caused by: org.python.core.PyException: ValueError: Length of values does not match length of paths.

... 26 common frames omitted

```

---

<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:** [August 25, 2023, 4:06pm UTC](https://forum.inductiveautomation.com/t/refresh-or-update-a-query-tag/78361/8 "2023-08-25T16:06:48Z")

</div>

Ah, you are passing a string and value to `writeBlocking` when it requires a list of strings and a list of values. Put square brackets around both of those to make them lists of length=one.

---

<div class="post-metadata">

**Author:** ![Omar\_Mohamed1](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@Omar\_Mohamed1](https://forum.inductiveautomation.com/u/Omar_Mohamed1)\
**Post date:** [August 25, 2023, 4:08pm UTC](https://forum.inductiveautomation.com/t/refresh-or-update-a-query-tag/78361/9 "2023-08-25T16:08:40Z")

</div>

and now it works, thanks for your help!

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [August 25, 2023, 4:25pm UTC](https://forum.inductiveautomation.com/t/refresh-or-update-a-query-tag/78361/10 "2023-08-25T16:25:38Z")

</div>

You should also _really_ update. You're apparently on a version older than 8.1. **3** , which was released Mar 3, 2021.

---

<div class="post-metadata">

**Author:** ![patrick.maltais](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/patrick.maltais/32/3958_2.png) [@patrick.maltais](https://forum.inductiveautomation.com/u/patrick.maltais)\
**Post date:** [February 2, 2024, 4:45pm UTC](https://forum.inductiveautomation.com/t/refresh-or-update-a-query-tag/78361/11 "2024-02-02T16:45:44Z")

</div>

I was able to trigger a manual refresh by addind a custom property to the QueryTag. If you configure the tag's Execution Mode to be Event Driven, the tag's query executes when something is referenced in the code. So by referencing the custom property in the code and changing it's value from a logic of your choice, you get the desired fucntionnality.  
In my case, I have a Query Tag named SomeQuery that only returns the actual Datetime. The tag has a custom property of type string named RefreshQuery. The query is event driven and references RefreshQuery. I placed a button in a Vision window or a Perspective View that writes the actual datetime to SomeQuery.RefreshQuery. When it changes, the query gets executed.  
Using a string and passing the datetime at the click event, you could probably find a way to use that time to prevent the query from executing too rapidly.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/6/c/6cc9b5cc9afe411c48d04515ef725afbf53beafe.png)  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/2/d/2d8e2de60bdbc18f30e26d8af7ab49c55f821a0e.png)

---

<div class="post-metadata">

**Author:** ![Eugene49](https://avatars.discourse-cdn.com/v4/letter/e/c6cbf5/32.png) [@Eugene49](https://forum.inductiveautomation.com/u/Eugene49)\
**Post date:** [August 11, 2024, 3:01pm UTC](https://forum.inductiveautomation.com/t/refresh-or-update-a-query-tag/78361/12 "2024-08-11T15:01:00Z")

</div>

> [@pturmel](#):
>
> Consider not using a query tag, but dataset memory tag. Run your query via scripting when necessary, and write the result to the memory tag.

On my project, on different views, and and on all open session. I send message to refresh binding to requerry (a query binding).

Instead of using query binding, I could just do a tag (with dataset type) binding? just write a new dataset to this tag and all may views and sessions will get refreshed?

---

<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:** [August 11, 2024, 3:38pm UTC](https://forum.inductiveautomation.com/t/refresh-or-update-a-query-tag/78361/13 "2024-08-11T15:38:56Z")

</div>

> [@Eugene49](#):
>
> Instead of using query binding, I could just do a tag (with dataset type) binding? just write a new dataset to this tag and all may views and sessions will get refreshed?

Yes this is correct. The bindings are subscribed to the tag so they will be updated when a tag value change occurs.

So long as the dataset is relatively small, this will work very well. The larger the dataset is the less I would recommend this approach.

Eliminating the messaging will also reduce the communications load on the gateway.

---

<div class="post-metadata">

**Author:** ![Alex\_Guyer1](https://avatars.discourse-cdn.com/v4/letter/a/5f8ce5/32.png) [@Alex\_Guyer1](https://forum.inductiveautomation.com/u/Alex_Guyer1)\
**Post date:** [October 22, 2024, 9:17pm UTC](https://forum.inductiveautomation.com/t/refresh-or-update-a-query-tag/78361/14 "2024-10-22T21:17:50Z")

</div>

I was able to use a method like this to get around the same problem.  
NOTE- The tag path as of version 8.1.43 at least is [.] not [~].  
Great solution for the edge cases that need this functionality. I would still like to see IA implement something better in the future so that the work around is not needed.

 ![Screenshot 2024-10-22 170825](https://us1.discourse-cdn.com/inductiveautomation/original/3X/7/d/7de5e574a8408b2515ee113ea16807a9f8c7e4f6.png)
