How to handle an Empty IN clause inside a SQL Sele

2019-09-06 10:16发布

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条回答
淡お忘
2楼-- · 2019-09-06 10:33

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>
查看更多
登录 后发表回答