# Query binding giving "Syntax Error on Token: 'End of Expression'" error, but works when testing the Named Query

**URL:** <https://forum.inductiveautomation.com/t/query-binding-giving-syntax-error-on-token-end-of-expression-error-but-works-when-testing-the-named-query/25832>\
**Category:** Ignition\
**Tags:** namedqueries\
**Created:** [June 14, 2019, 1:19pm UTC](https://forum.inductiveautomation.com/t/query-binding-giving-syntax-error-on-token-end-of-expression-error-but-works-when-testing-the-named-query/25832 "2019-06-14T13:19:02Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![md.szyman](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/md.szyman/32/9240_2.png) [@md.szyman](https://forum.inductiveautomation.com/u/md.szyman)\
**Post date:** [June 14, 2019, 1:19pm UTC](https://forum.inductiveautomation.com/t/query-binding-giving-syntax-error-on-token-end-of-expression-error-but-works-when-testing-the-named-query/25832/1 "2019-06-14T13:19:02Z")

</div>

![image](https://us1.discourse-cdn.com/inductiveautomation/original/2X/5/5d5daadc296b7d21636480fd5295239e6bcd1340.png)

For easier reading, here’s the query:

```sql
DECLARE @downtimeDivisor AS INT = 1

IF :downtimeFormat = 'hour' OR :downtimeFormat = 'hours'
	SET @downtimeDivisor = 3600;
ELSE IF :downtimeFormat = 'minute' OR :downtimeFormat = 'minutes'
	SET @downtimeDivisor = 60;

SELECT TOP(:limit)
	stops.first_fault AS fault,
	COUNT(*) AS count,
	(SUM(stops.stop_duration) / @downtimeDivisor) AS downtime
FROM stop_history stops
WHERE stops.first_fault_time BETWEEN :startDate AND :endDate
		AND stops.first_fault NOT IN {hiddenFaults}
GROUP BY stops.first_fault
ORDER BY count DESC

```

Shown above is a screenshot of my Query Binding setup. The Error I’m seeing is “Syntax Error on Token: ‘End of Expression’ (Line 0, Character 0)” and I’m completely stumped as to what’s causing it.

Queries with these same `startDate`, `endDate`, `limit` and `hiddenFaults` parameters are being used elsewhere with no problem. The main difference here between all my other queries is the use of `DECLARE` for local variables. Could that be causing a problem?

The more bizarre thing is that this query works just fine in the Named Queries query testing area, with manually provided parameters.

---

<div class="post-metadata">

**Author:** ![md.szyman](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/md.szyman/32/9240_2.png) [@md.szyman](https://forum.inductiveautomation.com/u/md.szyman)\
**Post date:** [June 14, 2019, 1:54pm UTC](https://forum.inductiveautomation.com/t/query-binding-giving-syntax-error-on-token-end-of-expression-error-but-works-when-testing-the-named-query/25832/2 "2019-06-14T13:54:42Z")

</div>

I’ve been making some changes and the error is different now. Now it’s “Syntax Error on Token: IDENTIFIER (Line 1, Character 43)”

If that’s to be believed, it’s at the end of the first “IF” statement on my updated query, here:

```sql
DECLARE @downtimeDivisor AS INT = 1

IF :downtimeFormat = 'hours'
	SET @downtimeDivisor = 3600;
ELSE IF :downtimeFormat = 'minutes'
	SET @downtimeDivisor = 60;

SELECT TOP(:limit)
	stops.first_fault AS fault,
	COUNT(*) AS count,
	(SUM(stops.stop_duration) / @downtimeDivisor) AS downtime
FROM stop_history stops
WHERE stops.first_fault_time BETWEEN :startDate AND :endDate
		AND stops.first_fault NOT IN {hiddenFaults}
GROUP BY stops.first_fault
ORDER BY count DESC

```

Again, this error is only occurring in the binding, the Named Query runs just fine when I test it in the Testing tab.

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [June 14, 2019, 2:26pm UTC](https://forum.inductiveautomation.com/t/query-binding-giving-syntax-error-on-token-end-of-expression-error-but-works-when-testing-the-named-query/25832/3 "2019-06-14T14:26:39Z")

</div>

It might not be a problem with the query, but rather the parameters you’re providing. trying removing the values from the “Value” column, then re-applying them, and taking extra care to hit Enter after you are done supplying each value.

If you are using the GUI to select property and/or tag values from the tree, the value isn’t actually supplied to the query until you then also hit Enter.

---

<div class="post-metadata">

**Author:** ![md.szyman](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/md.szyman/32/9240_2.png) [@md.szyman](https://forum.inductiveautomation.com/u/md.szyman)\
**Post date:** [June 14, 2019, 2:30pm UTC](https://forum.inductiveautomation.com/t/query-binding-giving-syntax-error-on-token-end-of-expression-error-but-works-when-testing-the-named-query/25832/4 "2019-06-14T14:30:11Z")

</div>

Well, that fixed it.

I had tried completely removing and recreating the binding, but I guess I had made the same mistake twice when doing so.

Thanks!

---

<div class="post-metadata">

**Author:** ![james.vosler](https://avatars.discourse-cdn.com/v4/letter/j/8797f3/32.png) [@james.vosler](https://forum.inductiveautomation.com/u/james.vosler)\
**Post date:** [December 4, 2020, 4:00pm UTC](https://forum.inductiveautomation.com/t/query-binding-giving-syntax-error-on-token-end-of-expression-error-but-works-when-testing-the-named-query/25832/5 "2020-12-04T16:00:22Z")

</div>

String type parameters with string constant parameters will not work with surrounding quotes when testing the named query, but will ONLY work with surrounding quotes when used in a query binding. If the string constant parameter is provided without surrounding quotes in the query binding, it will result in Error\_Configuration(“RuntimeException: Syntax Error on Token: ‘End of Expression’ (Line 0, Character 0)”)

---

<div class="post-metadata">

**Author:** ![Kurt\_Schron](https://avatars.discourse-cdn.com/v4/letter/k/ea666f/32.png) [@Kurt\_Schron](https://forum.inductiveautomation.com/u/Kurt_Schron)\
**Post date:** [October 24, 2023, 7:28pm UTC](https://forum.inductiveautomation.com/t/query-binding-giving-syntax-error-on-token-end-of-expression-error-but-works-when-testing-the-named-query/25832/6 "2023-10-24T19:28:49Z")

</div>

Thanks @james.vosler This was driving me crazy. Is there a reason for this or just an issue?

---

<div class="post-metadata">

**Author:** ![james.vosler](https://avatars.discourse-cdn.com/v4/letter/j/8797f3/32.png) [@james.vosler](https://forum.inductiveautomation.com/u/james.vosler)\
**Post date:** [November 2, 2023, 5:00pm UTC](https://forum.inductiveautomation.com/t/query-binding-giving-syntax-error-on-token-end-of-expression-error-but-works-when-testing-the-named-query/25832/7 "2023-11-02T17:00:53Z")

</div>

I presume they're based on separate libraries under the hood with different assumptions. I'd prefer them to be consistent, or barring that to have a useful error message. I'll defer to @cmallonee for any real answers here.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [November 2, 2023, 5:09pm UTC](https://forum.inductiveautomation.com/t/query-binding-giving-syntax-error-on-token-end-of-expression-error-but-works-when-testing-the-named-query/25832/8 "2023-11-02T17:09:10Z")

</div>

> [@james.vosler](#):
>
> String type parameters with string constant parameters will not work with surrounding quotes when testing the named query

In the testing panel in the named query editor, you're passing literal strings that will be placed directly into the query - so `"some string"` is getting to your DB as a string literal _including the quotes_.

In a named query binding in Perspective, you're authoring _an expression_ that will be evaluated and substituted into your query - expressions don't allow bare quotes because it's a pseudo programming language, and it relies on the quotes to identify string literals.
