# COUNT Query Not Working

**URL:** <https://forum.inductiveautomation.com/t/count-query-not-working/9655>\
**Category:** Ignition\
**Created:** [February 4, 2015, 11:34pm UTC](https://forum.inductiveautomation.com/t/count-query-not-working/9655 "2015-02-04T23:34:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Automator59](https://avatars.discourse-cdn.com/v4/letter/a/13edae/32.png) [@Automator59](https://forum.inductiveautomation.com/u/Automator59)\
**Post date:** [February 4, 2015, 11:34pm UTC](https://forum.inductiveautomation.com/t/count-query-not-working/9655/1 "2015-02-04T23:34:25Z")

</div>

I am trying to get a count of the number of rows in a MS SQL database through a script query. I have a Popup Calendar object and when the user selects the date, it will check to see if that week is in the database (count \> 0), if it isn’t then a new record will be created. The query I have works in the database query browser, but returns an error when I try to run it. There will be a variable in place for the date to select once the code is completed. The script is in the propertyChange of the Popup Calendar object.

[code]if event.propertyName == “date”:  
dayOfWeek = system.db.dateFormat(event.newValue, “E”)  
weekStart = system.db.dateFormat(event.newValue, “YYYY-MM-DD”)

```
# Message If Sunday Not Selected
if dayOfWeek <> "Sun":
	system.gui.messageBox("Please Select A Sunday")
	event.source.date = event.oldValue
# Process If Sunday Selected
if dayOfWeek == "Sun":
	# Check if database date range exists
	weekExistQuery = "SELECT COUNT (*) AS numRows FROM tblProdSchData WHERE WeekStart = '2015-01-04'"
	numRows = system.db.runUpdateQuery(weekExistQuery)
	print "Rows:",numRows[/code]
```

---

<div class="post-metadata">

**Author:** ![Automator59](https://avatars.discourse-cdn.com/v4/letter/a/13edae/32.png) [@Automator59](https://forum.inductiveautomation.com/u/Automator59)\
**Post date:** [February 4, 2015, 11:35pm UTC](https://forum.inductiveautomation.com/t/count-query-not-working/9655/2 "2015-02-04T23:35:29Z")

</div>

The error message I am receiving is:  
Traceback (most recent call last):

File “event:propertyChange”, line 13, in

java.lang.Exception: java.lang.Exception: Error executing system.db.runUpdateQuery(SELECT COUNT (\*) AS numRows FROM tblProdSchData WHERE WeekStart = ‘2015-01-04’, , , false)

```
caused by Exception: Error executing system.db.runUpdateQuery(SELECT COUNT (*) AS numRows FROM tblProdSchData WHERE WeekStart = '2015-01-04', , , false)
caused by GatewayException: SQL error for "SELECT COUNT (*) AS numRows FROM tblProdSchData WHERE WeekStart = '2015-01-04'": A result set was generated for update.
caused by SQLServerException: A result set was generated for update.

```

Ignition v7.7.2 (b2014121709)  
Java: Oracle Corporation 1.8.0\_31

---

<div class="post-metadata">

**Author:** ![JGJohnson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jgjohnson/32/29582_2.png) [@JGJohnson](https://forum.inductiveautomation.com/u/JGJohnson)\
**Post date:** [February 4, 2015, 11:43pm UTC](https://forum.inductiveautomation.com/t/count-query-not-working/9655/3 "2015-02-04T23:43:28Z")

</div>

Why are you using runUpdateQuery?

I think the best option would be to use [runScalarQuery](https://support.inductiveautomation.com/usermanuals/ignition/system_db_runscalarquery.htm)
