Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using case inside where clause

Tags:

sql

oracle

plsql

I´m trying to create a procedure that has a parameter called m_reaplicacao. This parameter receives the values 'S' for Yes, 'N' for No and 'T' for all records. When the parameter is Yes, I should return the records with value equals to 9. When the parameter is No, I should return the records different of 9. And finally, when the the value is All, I should return all records from the table. With the code bellow, Oracle says: Compilation errors for PROCEDURE MYDB.CONTAS_A_PAGAR_SPS Error: PL/SQL: ORA-00905: missing keyword Line: 84 Text: ta.id_1a_cbr = 9;

    select * from proposta ta
    where 
          ta.estado = 'RJ'
          and case
               when m_reaplicacao = 'S' then
                    ta.id_1a_cbr = 9;
               when m_reaplicacao = 'N' then
                    ta.id_1a_cbr <> 9
               else null
          end case; 

I saw a lot of posts, but I did not solve this one. Can someone help me, please?

like image 462
Rodrigo Souza Avatar asked Jun 16 '14 17:06

Rodrigo Souza


People also ask

Can I use a case statement in a WHERE clause?

CASE can be used in any statement or clause that allows a valid expression. For example, you can use CASE in statements such as SELECT, UPDATE, DELETE and SET, and in clauses such as select_list, IN, WHERE, ORDER BY, and HAVING.

Can we use 2 conditions in WHERE clause?

You can specify multiple conditions in a single WHERE clause to, say, retrieve rows based on the values in multiple columns. You can use the AND and OR operators to combine two or more conditions into a compound condition.

Can we use CASE in WHERE clause in Oracle?

You can use a CASE expression in any statement or clause that accepts a valid expression. For example, you can use the CASE expression in statements such as SELECT , UPDATE , or DELETE , and in clauses like SELECT , WHERE , HAVING , and ORDDER BY .

Can we use CASE inside case?

CASE can be nested in another CASE as well as in another IF…ELSE statement. In addition to SELECT, CASE can be used with another SQL clause like UPDATE, ORDER BY.


2 Answers

Don't use a CASE statement in a WHERE clause when you really want a simple combination of boolean evaluations.

WHERE ta.estado = 'RJ'
  AND (    m_reaplicacao = 'T'
       OR (m_reaplicacao = 'S' AND ta.id_1a_cbr = 9)
       OR (m_reaplicacao = 'N' AND ta.id_1a_cbr <> 9)
      )

If for some reason you really do want to use a CASE statement, you'd need the CASE to return a value that you check in the WHERE clause. For example

WHERE ta.estado = 'RJ'
  AND (CASE WHEN m_reaplicacao = 'S' AND ta.id_1a_cbr = 9 
            THEN 1
            WHEN m_reaplicacao = 'N' AND ta.id_1a_cbr <> 9 
            THEN 1
            WHEN m_reaplicacao = 'T'
            THEN 1
            ELSE 2
        END) = 1

This is not generally the clearest way to express this sort of condition, however.

like image 57
Justin Cave Avatar answered Oct 06 '22 20:10

Justin Cave


You cannot return expressions in CASE statements, easiest to add additional WHERE criteria sets:

select * 
from proposta ta
where ta.estado = 'RJ'
  and (
         (m_reaplicacao = 'S' AND ta.id_1a_cbr = 9)
      OR (m_reaplicacao = 'N' AND ta.id_1a_cbr <> 9)
       )

Not sure what you want to happen in the NULL situation.

like image 26
Hart CO Avatar answered Oct 06 '22 21:10

Hart CO