Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I getting ORA-06592: CASE not found while executing CASE statement in PL/SQL?

Tags:

oracle

plsql

I have the following CASE in PL/SQL

        CASE
            WHEN v_line_item.custom_segment = 'CND1' THEN
                v_current_col := v_col_lcy_tps;
            WHEN v_line_item.custom_segment = 'CND2' THEN
                v_current_col := v_col_lcy_ib;
            WHEN v_line_item.custom_segment = 'CND3' THEN
                v_current_col := v_col_lcy_gm;
            WHEN v_line_item.custom_segment = 'CND4' THEN
                v_current_col := v_col_lcy_pb;
            WHEN v_line_item.custom_segment = 'CND5' THEN
                v_current_col := v_col_lcy_bb;
        END CASE;

The code compiles fine, but when I execute to stored proc I get the following error:

ORA-06592: CASE not found while executing CASE statement

So when I remove the CASE; the stored proc won't compile. The only Examples I can get my hands on, uses the CASE in a select statement, I don't want to use it in select statement, I want to set my variable without having a bunch of IF THEN ELSE statements.

like image 745
Marthinus Avatar asked Aug 29 '11 06:08

Marthinus


People also ask

How is a CASE statement executed in Oracle?

The CASE statement selects a sequence of statements to execute. To select the sequence, the CASE statement uses a selector (an expression whose value is used to select one of several alternatives) or, in the searched CASE statement, multiple search conditions.

Does PL SQL have CASE statement?

The PL/SQL CASE statement facilitates you to execute a sequence of satatements based on a selector. A selector can be anything such as variable, function or an expression that the CASE statement checks to a boolean value. The CASE statement works like the IF statement, only using the keyword WHEN.

What are the two types of case statements in PL SQL?

The CASE statement has two types: simple CASE statement and searched CASE statement. Both types of the CASE statements support an optional ELSE clause.

Can you nest case statements in Oracle SQL?

Can CASE statements be nested? type_pre, As we see, Oracle allows nested CASE operators.


1 Answers

If you use a CASE statement - the listings under the CASE - must match all conditions that you might encounter - either explicitly as you have done by using

WHEN v_line_item.custom_segment = 'CND1' THEN
            v_current_col := v_col_lcy_tps;
WHEN v_line_item.custom_segment = 'CND2' THEN

or by using the ELSE clause.

Your code is hitting a situation where v_line_item.custom_segment doesn't match any of the given CASE scenarios, hence Oracle raises this exception.

You could add a catch-all condition

ELSE
   -- do some work here, raise an exception or log it.

so that it matches all conditions.

Further reading:

  • What you must know about CASE in PL/SQL
  • Oracle Documentation on CASE
like image 56
Sathyajith Bhat Avatar answered Sep 20 '22 14:09

Sathyajith Bhat