# Run named query and move data to OPC tags

**URL:** <https://forum.inductiveautomation.com/t/run-named-query-and-move-data-to-opc-tags/74404>\
**Category:** Ignition\
**Tags:** ignition81, namedqueries, scripting\
**Created:** [June 1, 2023, 12:47am UTC](https://forum.inductiveautomation.com/t/run-named-query-and-move-data-to-opc-tags/74404 "2023-06-01T00:47:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![imt.sebastian.sanche](https://avatars.discourse-cdn.com/v4/letter/i/ecb155/32.png) [@imt.sebastian.sanche](https://forum.inductiveautomation.com/u/imt.sebastian.sanche)\
**Post date:** [June 1, 2023, 12:47am UTC](https://forum.inductiveautomation.com/t/run-named-query-and-move-data-to-opc-tags/74404/1 "2023-06-01T00:47:37Z")

</div>

Hi, I have a project where I need to run a named query then from the columns grab the data and assign it to OPC tags.  
So far I tried to run the query from the scrip console but it return "none". So it isn't running the query?

I have the script attached to a tag and it runs on change.

| | parameters = { Sequence : currentValue } |
| --- | --- |
| | #Query call and store results |
| | qresult = system.db.runNamedQuery(Vision\_OC, parameters) |
| | |
| | #Place Results in Tags |
| | [.]VIN = qresult.getValueAt(1, 1) |

At this moment the error I'm getting is that I can't assign the result to a string. Any help would be appreciated.

---

<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 1, 2023, 12:48am UTC](https://forum.inductiveautomation.com/t/run-named-query-and-move-data-to-opc-tags/74404/2 "2023-06-01T00:48:40Z")

</div>

> [@imt.sebastian.sanche](#):
>
> [.]VIN = qresult.getValueAt(1, 1)

This is not valid syntax in a script. You will need to use `system.tag.writeBlocking()`.

Don't forget to use quotes around the string constants in your script.

---

<div class="post-metadata">

**Author:** ![imt.sebastian.sanche](https://avatars.discourse-cdn.com/v4/letter/i/ecb155/32.png) [@imt.sebastian.sanche](https://forum.inductiveautomation.com/u/imt.sebastian.sanche)\
**Post date:** [June 1, 2023, 1:03am UTC](https://forum.inductiveautomation.com/t/run-named-query-and-move-data-to-opc-tags/74404/3 "2023-06-01T01:03:49Z")

</div>

So I looked into your recommendation and I have no errors on the script at least that Ignition would let me know but when I run it my tags don't change value, went to the gateway logs and this is what is shows:

> com.inductiveautomation.ignition.common.script.JythonExecException: java.lang.IllegalArgumentException: java.lang.IllegalArgumentException: Project name required for script execution: the Gateway Scripting Project is not set.

My Code now looks as follow:

> parameters = { "Sequence" : currentValue }  
> #Query call and store results  
> qresult = system.db.runNamedQuery("Vision\_OC", parameters)  
> #Place Results in Tags  
> paths =["[default]Folder/Folder/Tag","[default]Folder/Folder/Tag"]  
> valueA = qresult.getValueAt(1,1)  
> valueB = qresult.getValueAt(1,15)  
> values = [valueA, valueB]  
> system.tag.writeBlocking(paths, values)

---

<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 1, 2023, 1:08am UTC](https://forum.inductiveautomation.com/t/run-named-query-and-move-data-to-opc-tags/74404/4 "2023-06-01T01:08:15Z")

</div>

{ Please use the "Preformatted text" editor button instead of the block quote button for code snippets. }

`system.db.runNamedQuery()` has two different sets of arguments, based on the calling scope. You are trying to use the syntax for Designer or Vision Client scope, but are apparently calling from a tag event (gateway scope).

Tags do not belong to projects, so many such calls from gateway scope must specify a project name (where to find the named query).

---

<div class="post-metadata">

**Author:** ![imt.sebastian.sanche](https://avatars.discourse-cdn.com/v4/letter/i/ecb155/32.png) [@imt.sebastian.sanche](https://forum.inductiveautomation.com/u/imt.sebastian.sanche)\
**Post date:** [June 1, 2023, 1:53am UTC](https://forum.inductiveautomation.com/t/run-named-query-and-move-data-to-opc-tags/74404/5 "2023-06-01T01:53:02Z")

</div>

First of apologies, the preformatted test was hidden in the cog, now I know. So it is still not running but now with a different error.

> com.inductiveautomation.ignition.common.script.JythonExecException: java.lang.Exception: java.lang.Exception: com.microsoft.sqlserver.jdbc.SQLServerException: Conversion failed when converting the \*\*\*\*\*\* value '\*\*\*\*\*\*' to data type \*\*\*\*\*\*.

So now there is an issue with the data type? So I checked tag is set to string but it is only numbers, if I type the numbers it will still recognize it as a string correct? could that be the data type? the column 1 and 15 of the query both are string and the example for the `sytem.tag.writeBlocking()` shows inserting the values to a list.

```auto
parameters = { "Sequence" : currentValue }
	#Query call and store results
	qresult = system.db.runNamedQuery("CognexVisionSystem", "Vision_OC", parameters)	
	#Place Results in Tags
	paths =["[default]CognexEOLInspection/AD01_Cognex/VIN","[default]CognexEOLInspection/AD01_Cognex/Logo"]
	valueA = qresult.getValueAt(1,1)
	valueB = qresult.getValueAt(1,15)
	values = [valueA, valueB]
	system.tag.writeBlocking(paths, values)type or paste code here

```

---

<div class="post-metadata">

**Author:** ![nideyijuyidong](https://avatars.discourse-cdn.com/v4/letter/n/7cd45c/32.png) [@nideyijuyidong](https://forum.inductiveautomation.com/u/nideyijuyidong)\
**Post date:** [June 1, 2023, 7:22am UTC](https://forum.inductiveautomation.com/t/run-named-query-and-move-data-to-opc-tags/74404/6 "2023-06-01T07:22:26Z")

</div>

**currentValue** is **qualified values** ,

> The currentValue and previousValue arguments are qualified values: objects that contain a value, timestamp, and quality. This means that to get to the value of the currentValue, your script would need to access `currentValue.value`
