# IBM DB2 Named Query won't Run

**URL:** <https://forum.inductiveautomation.com/t/ibm-db2-named-query-wont-run/21674>\
**Category:** Ignition\
**Created:** [January 3, 2019, 7:18pm UTC](https://forum.inductiveautomation.com/t/ibm-db2-named-query-wont-run/21674 "2019-01-03T19:18:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [January 3, 2019, 7:18pm UTC](https://forum.inductiveautomation.com/t/ibm-db2-named-query-wont-run/21674/1 "2019-01-03T19:18:41Z")

</div>

I’m trying to run a named query on a IBM DB2 db, but I keep getting an error. The query runs perfect while using the testing tab. I have table bindings that uses named queries to this same db without issue. When I try to run the query through a button (just for testing), I get this error

```auto
Traceback (most recent call last):

  File "<event:actionPerformed>", line 2, in <module>

com.inductiveautomation.ignition.client.gateway_interface.GatewayException: com.inductiveautomation.ignition.client.gateway_interface.GatewayException: com.ibm.as400.access.AS400JDBCSQLSyntaxErrorException: [SQL0199] Keyword FETCH not expected. Valid tokens: FOR USE SKIP WAIT WITH OPTIMIZE. Cause . . . . . : The keyword FETCH was not expected here. A syntax error was detected at keyword FETCH. The partial list of valid tokens is FOR USE SKIP WAIT WITH OPTIMIZE. This list assumes that the statement is correct up to the unexpected keyword. The error may be earlier in the statement but the syntax of the statement seems to be valid up to this point. Recovery . . . : Examine the SQL statement in the area of the specified keyword. A colon or SQL delimiter may be missing. SQL requires reserved words to be delimited when they are used as a name. Correct the SQL statement and try the request again.

	caused by GatewayException: com.ibm.as400.access.AS400JDBCSQLSyntaxErrorException: [SQL0199] Keyword FETCH not expected. Valid tokens: FOR USE SKIP WAIT WITH OPTIMIZE. Cause . . . . . : The keyword FETCH was not expected here. A syntax error was detected at keyword FETCH. The partial list of valid tokens is FOR USE SKIP WAIT WITH OPTIMIZE. This list assumes that the statement is correct up to the unexpected keyword. The error may be earlier in the statement but the syntax of the statement seems to be valid up to this point. Recovery . . . : Examine the SQL statement in the area of the specified keyword. A colon or SQL delimiter may be missing. SQL requires reserved words to be delimited when they are used as a name. Correct the SQL statement and try the request again.
	caused by Exception: com.ibm.as400.access.AS400JDBCSQLSyntaxErrorException: [SQL0199] Keyword FETCH not expected. Valid tokens: FOR USE SKIP WAIT WITH OPTIMIZE. Cause . . . . . : The keyword FETCH was not expected here. A syntax error was detected at keyword FETCH. The partial list of valid tokens is FOR USE SKIP WAIT WITH OPTIMIZE. This list assumes that the statement is correct up to the unexpected keyword. The error may be earlier in the statement but the syntax of the statement seems to be valid up to this point. Recovery . . . : Examine the SQL statement in the area of the specified keyword. A colon or SQL delimiter may be missing. SQL requires reserved words to be delimited when they are used as a name. Correct the SQL statement and try the request again.

Ignition v7.9.10 (b2018112821)
Java: Oracle Corporation 1.8.0_191

```

this is the query:

```auto
SELECT PDPLNT
FROM PURCHASE.PURCHD
WHERE PDVEND = 808 and PDRECD = 0 and PDCMPL != 'C' and PDNUMB = :po
LIMIT 1

```

and my code to run the query:

```auto
system.db.runNamedQuery("BOM/PlantLookup", {'po':'129902'})

```

EDIT: I should also mention that this db is not my default for this project, I do have it selected as the db to run on in the named query section

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [January 3, 2019, 7:30pm UTC](https://forum.inductiveautomation.com/t/ibm-db2-named-query-wont-run/21674/2 "2019-01-03T19:30:59Z")

</div>

Solved: It didn’t like the  
`LIMIT 1`  
I had to change it to  
`GROUP BY PDPLNT`

---

<div class="post-metadata">

**Author:** ![jtx2](https://avatars.discourse-cdn.com/v4/letter/j/a183cd/32.png) [@jtx2](https://forum.inductiveautomation.com/u/jtx2)\
**Post date:** [May 21, 2022, 3:04pm UTC](https://forum.inductiveautomation.com/t/ibm-db2-named-query-wont-run/21674/3 "2022-05-21T15:04:07Z")

</div>

The solution is a workaround, SQL via DB2 should be able to use LIMIT and FETCH FIRST… both work when trying via the testing tab in Named Queries, but neither appear to work via the Query Browser or when the named query is called via a script… tested in v7.9.20. This is the error: “Caused by: com.inductiveautomation.ignition.client.gateway\_interface.GatewayException: com.ibm.db2.jcc.am.mo: DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601, SQLERRMC=SELECT  
attribute\_value  
FROM  
;BEGIN-OF-STATEMENT;\<query\_expr\_body\>, DRIVER=4.7.85”
