# Error when querying for cases by caseDefinitionId and caseInstanceId

**URL:** <https://forum.flowable.org/t/error-when-querying-for-cases-by-casedefinitionid-and-caseinstanceid/5389>\
**Category:** Flowable Engine\
**Created:** [February 6, 2020, 5:09pm UTC](https://forum.flowable.org/t/error-when-querying-for-cases-by-casedefinitionid-and-caseinstanceid/5389 "2020-02-06T17:09:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![DavidPLamas](https://avatars.discourse-cdn.com/v4/letter/d/58f4c7/32.png) [@DavidPLamas](https://forum.flowable.org/u/DavidPLamas)\
**Post date:** [February 6, 2020, 5:09pm UTC](https://forum.flowable.org/t/error-when-querying-for-cases-by-casedefinitionid-and-caseinstanceid/5389/1 "2020-02-06T17:09:26Z")

</div>

It’s a bit of a dumb thing to do, but an exception is being raised if I execute the following code (Flowable 6.5). I know it doesn’t make any sense to do this query, I’m just testing my REST API on top of Flowable’s Java API.  
It probably makes more sense to throw a FlowableException when trying to use both methods instead of letting it reach the database.

```java
cmmnRuntimeService.createCaseInstanceQuery()
.caseInstanceId("caseInstanceId")
.caseDefinitionId("caseDefinitionId");

```

```auto
2020-02-06 17:02:10.176 [ERROR] [default task-1] CommandContext - Error while closing command context
org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database. Cause: com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near 'RES'.
### The error may exist in org/flowable/cmmn/db/mapping/entity/CaseInstance.xml
### The error may involve org.flowable.cmmn.engine.impl.persistence.entity.CaseInstanceEntityImpl.selectCaseInstancesByQueryCriteria-Inline
### The error occurred while setting parameters
### SQL: SELECT SUB.* FROM ( select distinct RES.* , row_number() over (order by RES.ID_ asc) rnk FROM ( select distinct RES.* , CASE_DEF.KEY_ as CaseDefinitionKey, CASE_DEF.ID_ as CaseDefinitionId, CASE_DEF.NAME_ as CaseDefinitionName, CASE_DEF.VERSION_ as CaseDefinitionVersion, CASE_DEF.DEPLOYMENT_ID_ as DeploymentId from ACT_CMMN_RU_CASE_INST RES inner join ACT_CMMN_CASEDEF CASE_DEF on RES.CASE_DEF_ID_ = CASE_DEF.ID_ WHERE RES.ID_ = ? RES.CASE_DEF_ID_ = ? )RES ) SUB WHERE SUB.rnk >= ? AND SUB.rnk < ?
### Cause: com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near 'RES'.

```

---

<div class="post-metadata">

**Author:** ![joram](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.flowable.org/joram/32/26_2.png) [@joram](https://forum.flowable.org/u/joram)\
**Post date:** [February 17, 2020, 10:36am UTC](https://forum.flowable.org/t/error-when-querying-for-cases-by-casedefinitionid-and-caseinstanceid/5389/2 "2020-02-17T10:36:26Z")

</div>

True, the query doesn’t make much sense (as the caseInstanceId is already unique), but it should work nevertheless. Fixed here: [https://github.com/flowable/flowable-engine/commit/05280e86e16811a37368837a406c3e3b3a1b0159](https://github.com/flowable/flowable-engine/commit/05280e86e16811a37368837a406c3e3b3a1b0159)
