# How to take first row of a querry tag and write it to a similar memory dataset tag? on Value Changed script

**URL:** <https://forum.inductiveautomation.com/t/how-to-take-first-row-of-a-querry-tag-and-write-it-to-a-similar-memory-dataset-tag-on-value-changed-script/29369>\
**Category:** Ignition\
**Created:** [October 24, 2019, 6:05pm UTC](https://forum.inductiveautomation.com/t/how-to-take-first-row-of-a-querry-tag-and-write-it-to-a-similar-memory-dataset-tag-on-value-changed-script/29369 "2019-10-24T18:05:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![wking](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/wking/32/21823_2.png) [@wking](https://forum.inductiveautomation.com/u/wking)\
**Post date:** [October 24, 2019, 6:05pm UTC](https://forum.inductiveautomation.com/t/how-to-take-first-row-of-a-querry-tag-and-write-it-to-a-similar-memory-dataset-tag-on-value-changed-script/29369/1 "2019-10-24T18:05:25Z")

</div>

```auto
def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):
	"""
	Fired whenever the current value changes in value or quality.

	Arguments:
		tag: The source tag object. A read-only wrapper for obtaining tag
		     properties.
		tagPath: The full path to the tag (String)
		previousValue: The previous value. This is a "qualified value", so it
		               has value, quality, and timestamp properties.
		currentValue: The current value. This is a "qualified value", so it has
		              value, quality, and timestamp properties.
		initialChange: A boolean flag indicating whether this event is due to
		               the first execution or initial subscription.
		missedEvents: A flag indicating that some events have been skipped due
		              to event overflow.
	"""
	var = system.tag.read("[.]/Querry")
	data = system.dataset.toPyDataSet(var.value)
	system.tag.write("[.]/Data/Lots", data)

```

So i need to read the querry tag and append only the first row of memory tag.

---

<div class="post-metadata">

**Author:** ![wking](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/wking/32/21823_2.png) [@wking](https://forum.inductiveautomation.com/u/wking)\
**Post date:** [October 24, 2019, 11:46pm UTC](https://forum.inductiveautomation.com/t/how-to-take-first-row-of-a-querry-tag-and-write-it-to-a-similar-memory-dataset-tag-on-value-changed-script/29369/2 "2019-10-24T23:46:19Z")

</div>

```auto

	qset = system.tag.read("[.]/Querry")
	dset = system.tag.read("[.]/Data/Lots")
	qdata = system.dataset.toPyDataSet(qset.value)
	ddata = system.dataset.toPyDataSet(dset.value)
	system.tag.write("[.]/Data/Lots", qdata)
	fr = []
	for col in range(qdata.getColumnCount()):
		fr.append(qdata.getValueAt(0, col))
	print fr 

```

returns what i need kinda i guess the l is for long datatype:

```auto
[u'19D03297B013', 16L, 2019-10-24 18:41:45.0, 2019-10-24 18:43:10.0, 1970-01-01 00:01:25.0, u'038']

```

now how to go about writing that to just the first row of the data/lots tag?

---

<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:** [October 25, 2019, 12:01am UTC](https://forum.inductiveautomation.com/t/how-to-take-first-row-of-a-querry-tag-and-write-it-to-a-similar-memory-dataset-tag-on-value-changed-script/29369/3 "2019-10-25T00:01:20Z")

</div>

If you just want the first row from the source tag, the simplest way is to construct another dataset that has the other rows deleted. Ignition can do that with [system.dataset.deleteRows()](https://docs.inductiveautomation.com/display/DOC80/system.dataset.deleteRows). Something like this:

```auto
qs = system.tag.read("[.]Querry").value
ds = system.dataset.deleteRows(qs, range(1, qs.rowCount))
system.tag.write("[.]/Data/Lots", ds)

```

That’s it. Note that datasets are supposed to be immutable – you don’t modify them, you make new ones with the change(s) you want. Also, you don’t need a PyDataset unless you wish to loop through its rows with a simple `for` loop.

---

<div class="post-metadata">

**Author:** ![wking](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/wking/32/21823_2.png) [@wking](https://forum.inductiveautomation.com/u/wking)\
**Post date:** [October 25, 2019, 12:12am UTC](https://forum.inductiveautomation.com/t/how-to-take-first-row-of-a-querry-tag-and-write-it-to-a-similar-memory-dataset-tag-on-value-changed-script/29369/4 "2019-10-25T00:12:04Z")

</div>

If i wrote that to a separate tag.

then wanted to take that dataset with one row and update the target tags first row.

Or maybe I am way off.

I have a large querry that gets dumped into this source tag.

Then writing to the mem tag for a table. That they are going to do work against. (selecting the rows and making print jobs and writing back to a diff db)

always on row[1] and below.

Am i going the wrong way about this?

---

<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:** [October 25, 2019, 12:23am UTC](https://forum.inductiveautomation.com/t/how-to-take-first-row-of-a-querry-tag-and-write-it-to-a-similar-memory-dataset-tag-on-value-changed-script/29369/5 "2019-10-25T00:23:27Z")

</div>

> [@wking](#):
>
> Am i going the wrong way about this?

I think so. Your mem tag is going to get clobbered every time the original query runs. Editing tables in place is tricky with bindings. Consider just showing the source query to your users in a read-only table. (With the query right there on the table, not a tag.) When they pick a row other than the first, enable the buttons and/or editable fields for their task. When that task is done, script an update query on the source table (marking that row done or whatever), then fire a refresh on the table's sql binding so that row will disappear.

If the task involves editing some of that row data, do it one at a time, and consider using the raw/dirty technique described here:

> [@Bidirectional SQL binding firing too soon](http://forum.inductiveautomation.com/t/bidirectional-sql-binding-firing-too-soon/28953/4):
>
> The technique I use in Ignition to distinguish regular binding traffic from user input is to never bind a user input field directly to a source of data. I always place a custom property of the appropriate type on that field, usually named “raw”, and bind that to the source. Bidirectionally if a tag. Then I bind the input field’s editable property uni-directionally to the raw property. With the above, a property change script that captures a change to the input field property first checks if …

{ What's with the two 'r's in query, btw? }

---

<div class="post-metadata">

**Author:** ![wking](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/wking/32/21823_2.png) [@wking](https://forum.inductiveautomation.com/u/wking)\
**Post date:** [October 25, 2019, 12:32am UTC](https://forum.inductiveautomation.com/t/how-to-take-first-row-of-a-querry-tag-and-write-it-to-a-similar-memory-dataset-tag-on-value-changed-script/29369/6 "2019-10-25T00:32:48Z")

</div>

> [@pturmel](#):
>
> { What’s with the two 'r’s in query, btw? }

hahahaha it's a typo in over 50 UDTs and I am to scared to change it.... Its been driving me wild. ![quer](https://us1.discourse-cdn.com/inductiveautomation/original/2X/3/3d8f1dea21729912d4977a47ae7ade161b4225c1.jpeg)

especially when it's right next to the little query.

It's only between you and me tho. As I am our only eng.

Thanks for the help!
