# Result of SQL query into a Contrologix PLC tag

**URL:** <https://forum.inductiveautomation.com/t/result-of-sql-query-into-a-contrologix-plc-tag/105347>\
**Category:** Ignition\
**Tags:** ignition81\
**Created:** [April 21, 2025, 7:28pm UTC](https://forum.inductiveautomation.com/t/result-of-sql-query-into-a-contrologix-plc-tag/105347 "2025-04-21T19:28:38Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![charles.munro](https://avatars.discourse-cdn.com/v4/letter/c/779978/32.png) [@charles.munro](https://forum.inductiveautomation.com/u/charles.munro)\
**Post date:** [April 21, 2025, 7:28pm UTC](https://forum.inductiveautomation.com/t/result-of-sql-query-into-a-contrologix-plc-tag/105347/1 "2025-04-21T19:28:38Z")

</div>

I have a query tag that returns the result I am looking for. I don't need an array or anything, it is a single value. I want to write this into a global tag in my contrologix. Watching inductive university topics about query tags, at the bottom of the interface it looks like this used to be built in, because there was a checkbox with some drop downs where you could select the device and tag. This no longer exists. How do I accomplish this?

---

<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:** [April 21, 2025, 7:31pm UTC](https://forum.inductiveautomation.com/t/result-of-sql-query-into-a-contrologix-plc-tag/105347/2 "2025-04-21T19:31:15Z")

</div>

Just use a ~~Tag valueChange~~ Gateway Tag Change event, on the query tag, in combination with `system.opc.writeValue()` to write the value to the PLC anytime the value changes.

---

<div class="post-metadata">

**Author:** ![charles.munro](https://avatars.discourse-cdn.com/v4/letter/c/779978/32.png) [@charles.munro](https://forum.inductiveautomation.com/u/charles.munro)\
**Post date:** [April 21, 2025, 7:37pm UTC](https://forum.inductiveautomation.com/t/result-of-sql-query-into-a-contrologix-plc-tag/105347/3 "2025-04-21T19:37:38Z")

</div>

Here is what I just tried and it didn't like it:

Select top 1 [NumericAnswer]  
From [Horizon].[dbo].[SpecimenResult]  
where [DefinitionID] = 55 and [NumericAnswer] is not NULL  
order by [BatchID] Desc;  
system.opc.writeValue([default]Inverter\_Room/StartingStress)

Just that last line is what I changed. It worked fine without it.

---

<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:** [April 21, 2025, 7:40pm UTC](https://forum.inductiveautomation.com/t/result-of-sql-query-into-a-contrologix-plc-tag/105347/4 "2025-04-21T19:40:39Z")

</div>

That is because, what you did is not what I suggested.

Instead you tried to use a scripting function in your SQL, and that doesn't work.

Instead, you need to add the function to ~~the valueChanged~~ Gateway Tag Change Scripting event triggered by the tag.

> **[system.opc.writeValue | Ignition User Manual](https://www.docs.inductiveautomation.com/docs/8.1/appendix/scripting-functions/system-opc/system-opc-writeValue)**
>
> Writes a value directly through an OPC server connection synchronously.

> **[Gateway Event Scripts | Ignition User Manual](https://www.docs.inductiveautomation.com/docs/8.1/platform/scripting/scripting-in-ignition/gateway-event-scripts#tag-change-scripts)**
>
> Gateway Event Scripts Overview

---

<div class="post-metadata">

**Author:** ![charles.munro](https://avatars.discourse-cdn.com/v4/letter/c/779978/32.png) [@charles.munro](https://forum.inductiveautomation.com/u/charles.munro)\
**Post date:** [April 21, 2025, 8:02pm UTC](https://forum.inductiveautomation.com/t/result-of-sql-query-into-a-contrologix-plc-tag/105347/5 "2025-04-21T20:02:56Z")

</div>

![script](https://us1.discourse-cdn.com/inductiveautomation/original/3X/1/f/1f91fcd9392928474c2c3df708fe8b4d5dd45673.png)  
I tried this and it didn't work. Am I doing something wrong with the syntax?

---

<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 21, 2025, 8:16pm UTC](https://forum.inductiveautomation.com/t/result-of-sql-query-into-a-contrologix-plc-tag/105347/6 "2025-04-21T20:16:49Z")

</div>

> [@lrose](#):
>
> Tag valueChange event, on the query tag, in combination with `system.opc.writeValue()` to write the value

Not safe in a tag valueChange event. That system call blocks, possibly for long time.

Use a gateway tag change event for such a script.

(Or, perhaps, use my Integration Toolkit's "Republish" tag actor, which works basically just like the old v7.9 functionality.)

---

<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:** [April 22, 2025, 11:32am UTC](https://forum.inductiveautomation.com/t/result-of-sql-query-into-a-contrologix-plc-tag/105347/7 "2025-04-22T11:32:43Z")

</div>

Yeah, I'm not sure why I thought that was an Async function.

I have edited previous posts for a more appropriate solution.

---

<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:** [April 22, 2025, 11:40am UTC](https://forum.inductiveautomation.com/t/result-of-sql-query-into-a-contrologix-plc-tag/105347/8 "2025-04-22T11:40:40Z")

</div>

> [@charles.munro](#):
>
> Am I doing something wrong with the syntax?

Yes, you are using three variables in that script which have not been defined.

I handed out some poor advice, which has been corrected now. Sorry about that.

One way to approach this is to use a Gateway Tag Event Script:

You assign the query tag as a trigger tag, and then in the script you would use a script like this:

```python
if not initialChange:
    server = "Ignition OPC UA Server"
    path = "OPC Path to the PLC Tag"

    # newValue is pre-defined by the event. It will hold
    # the new value of the tag which triggered the event. In this case
    # the value of the query tag.
    system.opc.writeValue(server,path,newValue.value)

```
