# Script - SQL query error

**URL:** <https://forum.inductiveautomation.com/t/script-sql-query-error/35275>\
**Category:** Ignition\
**Created:** [June 3, 2020, 3:47am UTC](https://forum.inductiveautomation.com/t/script-sql-query-error/35275 "2020-06-03T03:47:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![john.eichler](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/john.eichler/32/24632_2.png) [@john.eichler](https://forum.inductiveautomation.com/u/john.eichler)\
**Post date:** [June 3, 2020, 3:47am UTC](https://forum.inductiveautomation.com/t/script-sql-query-error/35275/1 "2020-06-03T03:47:52Z")

</div>

Hi All

The following code works perfectly in the script console. However, when this code is called in a tag event script, it generates an error (shown below).

The table contains columns for Batch, StartTime and StopTime. The script is intended to prevent over writing of a record. i.e. check if the batch number exists, if not, insert (with additional code).

What is the reason for the different behaviour? 1394 does exist in the Batch column. and 27 is the line starting with "exists=" Is there a better way to achieve the desired functionality? I have played with COALESCE and ISNULL without any luck.

Can anyone help please?

```auto
command = "SELECT COUNT(Batch) FROM hamjet_report.dbo.STS_test WHERE Batch = 1394"
exists=system.db.runScalarPrepQuery(command)

```

> Error executing script.  
> Traceback (most recent call last):  
> File "tagevent:valueChanged", line 27, in valueChanged  
> at com.inductiveautomation.ignition.common.script.builtin.AbstractDBUtilities.error(AbstractDBUtilities.java:362)  
> at com.inductiveautomation.ignition.common.script.builtin.AbstractDBUtilities.runScalarPrepQuery(AbstractDBUtilities.java:315)  
> at jdk.internal.reflect.GeneratedMethodAccessor93.invoke(Unknown Source)  
> at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)  
> at java.base/java.lang.reflect.Method.invoke(Unknown Source)  
> java.lang.Exception: java.lang.Exception: Error executing system.db.runScalarPrepQuery(SELECT COUNT(Batch) FROM hamjet\_report.dbo.STS\_test WHERE Batch = 1394, , [null], )

---

<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:** [June 3, 2020, 12:20pm UTC](https://forum.inductiveautomation.com/t/script-sql-query-error/35275/2 "2020-06-03T12:20:42Z")

</div>

You are using the “Prep” version of the scalar query and not providing substitution parameters. Place a question mark where 1394 is, then put that batch number in the list of parameters.

---

<div class="post-metadata">

**Author:** ![john.eichler](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/john.eichler/32/24632_2.png) [@john.eichler](https://forum.inductiveautomation.com/u/john.eichler)\
**Post date:** [June 4, 2020, 12:01am UTC](https://forum.inductiveautomation.com/t/script-sql-query-error/35275/3 "2020-06-04T00:01:28Z")

</div>

Many thanks! My code is now working. Do you know why my incorrect code worked in the script console?

---

<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:** [June 4, 2020, 12:31pm UTC](https://forum.inductiveautomation.com/t/script-sql-query-error/35275/4 "2020-06-04T12:31:30Z")

</div>

> [@john.eichler](#):
>
> why my incorrect code worked

No idea.

---

<div class="post-metadata">

**Author:** ![john.eichler](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/john.eichler/32/24632_2.png) [@john.eichler](https://forum.inductiveautomation.com/u/john.eichler)\
**Post date:** [June 4, 2020, 9:14pm UTC](https://forum.inductiveautomation.com/t/script-sql-query-error/35275/5 "2020-06-04T21:14:06Z")

</div>

OK. Cheers.
