# Update statement

**URL:** <https://forum.inductiveautomation.com/t/update-statement/10066>\
**Category:** Ignition\
**Created:** [May 28, 2015, 3:25pm UTC](https://forum.inductiveautomation.com/t/update-statement/10066 "2015-05-28T15:25:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![MoisesZV](https://avatars.discourse-cdn.com/v4/letter/m/e68b1a/32.png) [@MoisesZV](https://forum.inductiveautomation.com/u/MoisesZV)\
**Post date:** [May 28, 2015, 3:25pm UTC](https://forum.inductiveautomation.com/t/update-statement/10066/1 "2015-05-28T15:25:39Z")

</div>

Hi, I have this table created.

```auto
CREATE TABLE `prueba` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `fecha` datetime NOT NULL,
  `usuario` varchar(45) DEFAULT NULL,
  `notas` varchar(245) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;
```

I’m trying to update the “notas” column when my id equals to the last row. This code updates all my rows, not just the last one, the “SELECT MAX()” it’s not working.

```auto
notas = event.source.parent.getComponent('detalle falla').text
update = system.db.runPrepUpdate("UPDATE prueba SET notas = ? WHERE id = (SELECT MAX(id))",[notas])

```

---

<div class="post-metadata">

**Author:** ![nmudge](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nmudge/32/3394_2.png) [@nmudge](https://forum.inductiveautomation.com/u/nmudge)\
**Post date:** [May 28, 2015, 3:29pm UTC](https://forum.inductiveautomation.com/t/update-statement/10066/2 "2015-05-28T15:29:52Z")

</div>

Try this:

```auto
notas = event.source.parent.getComponent('detalle falla').text
update = system.db.runPrepUpdate("UPDATE prueba SET notas = ? WHERE id = (SELECT MAX(id) FROM prueba)",[notas])
```

---

<div class="post-metadata">

**Author:** ![MoisesZV](https://avatars.discourse-cdn.com/v4/letter/m/e68b1a/32.png) [@MoisesZV](https://forum.inductiveautomation.com/u/MoisesZV)\
**Post date:** [May 28, 2015, 3:33pm UTC](https://forum.inductiveautomation.com/t/update-statement/10066/3 "2015-05-28T15:33:48Z")

</div>

I tried that before, this is the error "You can’t specify target table ‘prueba’ for update in FROM clause  
"

---

<div class="post-metadata">

**Author:** ![nmudge](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nmudge/32/3394_2.png) [@nmudge](https://forum.inductiveautomation.com/u/nmudge)\
**Post date:** [May 28, 2015, 3:41pm UTC](https://forum.inductiveautomation.com/t/update-statement/10066/4 "2015-05-28T15:41:33Z")

</div>

Okay, right, try this:

```auto
notas = event.source.parent.getComponent('detalle falla').text
update = system.db.runPrepUpdate("UPDATE prueba SET notas = ? ORDER BY id DESC LIMIT 1",[notas])
```

---

<div class="post-metadata">

**Author:** ![MoisesZV](https://avatars.discourse-cdn.com/v4/letter/m/e68b1a/32.png) [@MoisesZV](https://forum.inductiveautomation.com/u/MoisesZV)\
**Post date:** [May 28, 2015, 4:26pm UTC](https://forum.inductiveautomation.com/t/update-statement/10066/5 "2015-05-28T16:26:25Z")

</div>

thank you nmudge, it worked, you are very helpful!, thanks again.
