oracle 为什么我会收到 ORA-06592:在 PL/SQL 中执行 CASE 语句时找不到 CASE?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7226648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 03:29:56  来源:igfitidea点击:

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

oracleplsql

提问by Marthinus

I have the following CASE in PL/SQL

我在 PL/SQL 中有以下 CASE

        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

ORA-06592: 执行 CASE 语句时未找到 CASE

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.

所以当我移除 CASE 时;存储过程不会编译。我可以得到的唯一示例,在 select 语句中使用 CASE,我不想在 select 语句中使用它,我想设置我的变量而没有一堆 IF THEN ELSE 语句。

回答by Sathyajith Bhat

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

如果您使用CASE语句 - 下的列表CASE必须匹配您可能遇到的所有条件 - 或者像您通过使用所做的那样明确

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 ELSEclause.

或通过使用ELSE子句。

Your code is hitting a situation where v_line_item.custom_segmentdoesn't match any of the given CASEscenarios, hence Oracle raises this exception.

您的代码遇到了v_line_item.custom_segment与任何给定CASE场景都不匹配的情况,因此 Oracle 引发了此异常。

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:

进一步阅读:

回答by William Robertson

Old thread I know, but wouldn't you just write it like this?

旧线程我知道,但你不会就这样写吗?

v_current_col :=
    case v_line_item.custom_segment
        when 'CND1' then v_col_lcy_tps
        when 'CND2' then v_col_lcy_ib
        when 'CND3' then v_col_lcy_gm
        when 'CND4' then v_col_lcy_pb
        when 'CND5' then v_col_lcy_bb
    end;

It's more concise and readable and it won't give an ORA-06592 error.

它更简洁易读,不会出现 ORA-06592 错误。