How to handle an Empty IN clause inside a SQL Select Statement (IBatis 2)?

一世执手 提交于 2019-12-13 04:48:56

问题


I have written the following SQL Statment in IBatis version 2:

<select id="mySelect" resultClass="long" >
    SELECT  COUNT(*)        
    FROM    myTable 
    WHERE   myTable.columnA IN
        <iterate property="myInClauseValues" open="(" close=")" conjunction=",">                    
                #myInClauseValues[]#                    
        </iterate>          
</select>

That statement works fine, if myInClauseValues (this is a ArrayList with Long) include at least one value. But if myInClauseValues is empty, I get an error message like this (I´m using an oracle database):

 Check the statement (query failed). 
 Cause: java.sql.SQLException: ORA-00936: Expression is missing 

回答1:


Use Ibatis dyanmic query be a correct way??
eg:

<select id="mySelect" resultClass="long" >
    SELECT  COUNT(*)        
    FROM    myTable 
   <where>
    <isNotEmpty prepend="AND" property="myInClauseValues" >
       myTable.columnA IN
       <iterate property="myInClauseValues" open="(" close=")" conjunction=",">                    
                    #myInClauseValues[]#                    
       </iterate>       
    </isNotEmpty>   
   </where>      
</select>


来源:https://stackoverflow.com/questions/28359831/how-to-handle-an-empty-in-clause-inside-a-sql-select-statement-ibatis-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!