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?
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.
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.
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 .
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With