# Copying named query results to tags based on column name

**URL:** <https://forum.inductiveautomation.com/t/copying-named-query-results-to-tags-based-on-column-name/23910>\
**Category:** Ignition\
**Created:** [April 8, 2019, 6:47am UTC](https://forum.inductiveautomation.com/t/copying-named-query-results-to-tags-based-on-column-name/23910 "2019-04-08T06:47:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ASF](https://avatars.discourse-cdn.com/v4/letter/a/c4cdca/32.png) [@ASF](https://forum.inductiveautomation.com/u/ASF)\
**Post date:** [April 8, 2019, 6:47am UTC](https://forum.inductiveautomation.com/t/copying-named-query-results-to-tags-based-on-column-name/23910/1 "2019-04-08T06:47:31Z")

</div>

Continuing the discussion from [Passing tag values to named query](http://forum.inductiveautomation.com/t/passing-tag-values-to-named-query/23050):

Now I want to go back the other way. I have a named query to which I pass a search term, and it returns a single row containing the following columns and their respective values. When I test the query using the Testing tab of the query builder, it returns something like this:

```auto
ProductCode Description WeightSP ActualWeight
   12345 Product ABC 10.0 10.2

```

Now, I want to call this query based on a tag change, and have it load the values into a set of tags which have the same names, within the `[default]PrevBatch` tag folder, i.e. my tags are:

```auto
[default]PrevBatch/ProductCode
[default]PrevBatch/Description
[default]PrevBatch/WeightSP
[default]PrevBatch/ActualWeight

```

So, I put a tag change event on my triggering tag and have it call the named query. It returns a dataset.

Is there an equally elegant way to write these values into the tags that match the column names returned by the query?

---

<div class="post-metadata">

**Author:** ![ASF](https://avatars.discourse-cdn.com/v4/letter/a/c4cdca/32.png) [@ASF](https://forum.inductiveautomation.com/u/ASF)\
**Post date:** [April 8, 2019, 7:14am UTC](https://forum.inductiveautomation.com/t/copying-named-query-results-to-tags-based-on-column-name/23910/2 "2019-04-08T07:14:01Z")

</div>

Answering my own question - eventually got my head around the fact that trying to use the `zip` function is exactly the opposite of what I need to do here, since I need to _separate_ lists to feed into the `system.tag.writeBlocking` function.

```auto
if currentValue.value and not initialChange:
	paths = []
	values = []
	data = system.db.runNamedQuery('My_ProjectName', 'My_QueryName', {'SearchTerm':'My_SearchTerm'})
	for col in range(data.getColumnCount()):
		paths.append(data.getColumnName(col))
		values.append(data.getValueAt(0,col))
	paths = ['[default]Process/PrevBatch/' + path for path in paths]
	system.tag.writeBlocking(paths,values)

```

Always interested in optimisations though, if anyone’s feeling creative!

---

<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 8, 2019, 11:31am UTC](https://forum.inductiveautomation.com/t/copying-named-query-results-to-tags-based-on-column-name/23910/3 "2019-04-08T11:31:59Z")

</div>

Since you seem to be familiar with list comprehensions, I’d use a few more, and take advantage of jython’s NetBeans automatic property syntax:

```auto
if currentValue.value and not initialChange:
	ds = system.db.runNamedQuery('My_ProjectName', 'My_QueryName', {'SearchTerm':'My_SearchTerm'})
	paths = ['[default]Process/PrevBatch/' + path for path in ds.columnNames]
	values = [ds.getValueAt(0, c) for c in range(ds.columnCount)]
	system.tag.writeBlocking(paths,values)

```

---

<div class="post-metadata">

**Author:** ![ASF](https://avatars.discourse-cdn.com/v4/letter/a/c4cdca/32.png) [@ASF](https://forum.inductiveautomation.com/u/ASF)\
**Post date:** [April 8, 2019, 10:46pm UTC](https://forum.inductiveautomation.com/t/copying-named-query-results-to-tags-based-on-column-name/23910/4 "2019-04-08T22:46:34Z")

</div>

Thanks Phil, that simplifies things somewhat and tests out correctly.
