javaoracle-databasemybatis-mapper

coalesce mybatis switch case


Iam using coalesce mybatis switch case in my query, where iam getting error like

Error querying database. Cause: java.sql.SQLException: ORA-01427: single-row subquery returns more than one row

this is my query

(select      
     (case when (coalesce(t1.col1,t2.col1, t1.col2, t1.col3) is null)
          then (select sysdate from dual) 
          else (coalesce(t1.col1,t2.col1, t1.col2, t1.col3)) 
     end  )  
from table1 t1
join table2 t2 
    on t1.id IN (t2.id))

Thanks in advance


Solution

  • Seems you have a lot of () but overall you should use = operator and not IN (t2.id) for join t2.id

    select      
         case when coalesce(t1.col1,t2.col1, t1.col2, t1.col3) is null
              then  sysdate 
              else coalesce(t1.col1,t2.col1, t1.col2, t1.col3) 
         end    
    from table1 t1
    join table2 t2  on t1.id = t2.id
    

    And looking at the code you posted in sample you have a select as a column result and this select return several rows, ( this raise the error). You also have a mixin of join syntax some based on explicit join syntax some based on old implicit join syntax based on comma separated table name and where condition. You should try using this

    <select id="Trigger" parameterType="hashmap" resultType="java.util.HashMap" flushCache="true"> 
    
        SELECT 
            select case when coalesce(table1.col1, table2.col2,table1.col3, table1.col4) is null 
            then  sysdate 
            else coalesce(table1.col1, table2.col2,table1.col3, table1.col4) end as "ProgressDate"
            , table3.id as "ID" 
            from table1 
            INNER join table2 on table1.id = table2.id 
            INNER JOIN table3 ON table1.id = table3.id 
            INNER JOIN table4 table2.action = table4.action 
            WHERE table3.transaction = #{inputvaluepassed} 
            
    </select>