带有 OR 的 Oracle CASE

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1099845/
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 02:01:09  来源:igfitidea点击:

Oracle CASE with OR

oracleplsql

提问by Rob van Wijk

Can we use CASEstatement in oraclewith ORlike this:

我们可以使用CASE在声明中甲骨文OR这样的:

CASE WHEN A > 0 OR B >0 THEN c=1 END;

I know we can use AND, but I get an error when I use OR. Can you please suggest something? Thanks.

我知道我们可以使用AND,但是当我使用OR. 你能提出一些建议吗?谢谢。

回答by akf

have you tried putting your OR statement in parens?

你有没有试过把你的 OR 语句放在括号中?

 CASE WHEN (A > 0 OR B >0) THEN c=1 END;

回答by Rob van Wijk

You posted a CASE expression, but named it a CASE statement. That's probably where the confusion comes from. The CASE expression is valid:

您发布了一个 CASE 表达式,但将其命名为 CASE 语句。这可能是混乱的来源。CASE 表达式有效:

SQL> declare
  2    bool boolean;
  3    a    int := 1;
  4    b    int := 0;
  5    c    int := 1;
  6  begin
  7    bool := CASE WHEN A > 0 OR B >0 THEN c=1 END;
  8    if bool is null
  9    then
 10      dbms_output.put_line('NULL');
 11    elsif bool
 12    then
 13      dbms_output.put_line('TRUE');
 14    else
 15      dbms_output.put_line('FALSE');
 16    end if;
 17  end;
 18  /
TRUE

PL/SQL procedure successfully completed.

But you probably meant to use the CASE-statement, which ends with "END CASE" instead of "END". An example:

但是您可能打算使用以“END CASE”而不是“END”结尾的 CASE 语句。一个例子:

SQL> declare
  2    a    int := 1;
  3    b    int := 0;
  4    c    int;
  5  begin
  6    case
  7    when a > 0 or b > 0 then
  8      c := 1;
  9    end case
 10    ;
 11    dbms_output.put_line(c);
 12  end;
 13  /
1

PL/SQL procedure successfully completed.

Regards, Rob.

问候,罗布。