# If SQL Query no results do something

**URL:** <https://forum.inductiveautomation.com/t/if-sql-query-no-results-do-something/22520>\
**Category:** Ignition\
**Created:** [February 12, 2019, 7:58pm UTC](https://forum.inductiveautomation.com/t/if-sql-query-no-results-do-something/22520 "2019-02-12T19:58:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![juang](https://avatars.discourse-cdn.com/v4/letter/j/9dc877/32.png) [@juang](https://forum.inductiveautomation.com/u/juang)\
**Post date:** [February 12, 2019, 7:58pm UTC](https://forum.inductiveautomation.com/t/if-sql-query-no-results-do-something/22520/1 "2019-02-12T19:58:32Z")

</div>

Hi, i’m trying to select a result if the query returns a value, , if it does not return a value then do something else. i have it working with this code. but im trying to find a better way. because it does not look good.

```
                  selectlog="""select palletid from rawmaterialslog where palletid=? and lotid = ?"""
                  selectlo=system.db.runPrepQuery(selectlog, [newValue, lotid])
		
		try:

```

################ if no results when trying to print it will fail, and it will go to Except, and then i run my code from there. but looking to avoid using the print ########  
print selectlo[0][0]  
system.gui.messageBox(“That Pallet was already Scanned, please choose another”)

```
		except:
                                 Do something.
```

---

<div class="post-metadata">

**Author:** ![ethomason](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ethomason/32/43338_2.png) [@ethomason](https://forum.inductiveautomation.com/u/ethomason)\
**Post date:** [February 12, 2019, 8:13pm UTC](https://forum.inductiveautomation.com/t/if-sql-query-no-results-do-something/22520/2 "2019-02-12T20:13:05Z")

</div>

Since you’re expecting just 1 value as a result I’d use [runScalarPrepQuery](https://docs.inductiveautomation.com/display/DOC79/system.db.runScalarPrepQuery) instead, which will return palletid to you and None if there is no pallet.

```auto
selectlog="""select palletid from rawmaterialslog where palletid=? and lotid = ?"""
selectlo=system.db.runScalarPrepQuery(selectlog, [newValue, lotid])

if selectlo is not None:
	print selectlo
else:
	do something else

```

---

<div class="post-metadata">

**Author:** ![juang](https://avatars.discourse-cdn.com/v4/letter/j/9dc877/32.png) [@juang](https://forum.inductiveautomation.com/u/juang)\
**Post date:** [February 12, 2019, 9:26pm UTC](https://forum.inductiveautomation.com/t/if-sql-query-no-results-do-something/22520/3 "2019-02-12T21:26:36Z")

</div>

thank you worked perfect.
