# SQL timing

**URL:** <https://forum.inductiveautomation.com/t/sql-timing/8386>\
**Category:** Ignition\
**Created:** [April 29, 2014, 6:32pm UTC](https://forum.inductiveautomation.com/t/sql-timing/8386 "2014-04-29T18:32:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tim](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/tim/32/3361_2.png) [@Tim](https://forum.inductiveautomation.com/u/Tim)\
**Post date:** [April 29, 2014, 6:32pm UTC](https://forum.inductiveautomation.com/t/sql-timing/8386/1 "2014-04-29T18:32:29Z")

</div>

I have a button that allows data to be reordered via a sort order field in a table. It works great, most of the time. Occasionally if clicking the move up or move down button rapidly it is possible to end up with two rows with the same sort order. This situation requires manual recovery. Is there any way to prevent this occurrence? I have tried adding a running flag, but that does not seem to have any effect.

From the actionPerformed on the order button:

[code]OrderNumber = event.source.parent.getComponent(‘Sequence Table’).OrderNumber  
data = event.source.parent.getComponent(‘Sequence Table’).data  
row = event.source.parent.getComponent(‘Sequence Table’).selectedRow  
Running = event.source.Running

if row \> 0 and Running != 1:  
event.source.Running = 1  
currentSort = data.getValueAt(row,“SortOrder”)  
#get previous record  
sql = “SELECT TOP 1 ORDER\_NUMBER FROM tblSequence WHERE SortOrder \< %d ORDER BY SortOrder DESC”  
omsPrev = system.db.runScalarQuery(sql %(currentSort))  
sql = “UPDATE tblSequence SET SortOrder = SortOrder + 1 WHERE ORDER\_NUMBER = ?”  
system.db.runPrepUpdate(sql, [omsPrev])  
sql = “UPDATE tblSequence SET SortOrder = SortOrder - 1 WHERE ORDER\_NUMBER = ?”  
system.db.runPrepUpdate(sql, [OrderNumber])

```
event.source.parent.getComponent('Sequence Table').selectedRow = row -1
system.db.refresh(event.source.parent,"Sequence")
event.source.Running = 0

```

[/code]

---

<div class="post-metadata">

**Author:** ![drewdin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/drewdin/32/3363_2.png) [@drewdin](https://forum.inductiveautomation.com/u/drewdin)\
**Post date:** [April 30, 2014, 12:40pm UTC](https://forum.inductiveautomation.com/t/sql-timing/8386/2 "2014-04-30T12:40:24Z")

</div>

why not use the running bit to disable the button until the change has been completed to prevent multiple clicks and a timing issue?

---

<div class="post-metadata">

**Author:** ![adamaustin](https://avatars.discourse-cdn.com/v4/letter/a/5f8ce5/32.png) [@adamaustin](https://forum.inductiveautomation.com/u/adamaustin)\
**Post date:** [April 30, 2014, 3:15pm UTC](https://forum.inductiveautomation.com/t/sql-timing/8386/3 "2014-04-30T15:15:38Z")

</div>

That sounds easier than my idea: Combine the three queries into one query.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [April 30, 2014, 7:12pm UTC](https://forum.inductiveautomation.com/t/sql-timing/8386/4 "2014-04-30T19:12:47Z")

</div>

Not sure, but an invokeLater might be used to process the event, then run the script.
