ORACLE IIF 声明

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

ORACLE IIF Statement

oracleternary-operatoriif

提问by user1050619

I get an error while writing the IIF stamtement, table and the statement given below,

我在编写 IIF 声明、表格和下面给出的语句时出错,

Statement:

陈述:

SELECT IIF(EMP_ID=1,'True','False') from Employee;

Error: 00907-missing right parantheses

错误:00907-缺少右括号

CREATE TABLE SCOTT.EMPLOYEE
(
  EMP_ID       INTEGER                          NOT NULL,
  EMP_FNAME    VARCHAR2(30 BYTE)                NOT NULL,
  EMP_LNAME    VARCHAR2(30 BYTE)                NOT NULL,
  EMP_ADDRESS  VARCHAR2(50 BYTE)                NOT NULL,
  EMP_PHONE    CHAR(10 BYTE)                    NOT NULL,
  EMP_GENDER   CHAR(1 BYTE)
)

Please provide your inputs.

请提供您的意见。

回答by Mt. Schneiders

Oracle doesn't provide such IIF Function. Instead, try using one of the following alternatives:

Oracle 没有提供这样的 IIF 函数。相反,请尝试使用以下替代方法之一:

DECODE Function:

解码功能

SELECT DECODE(EMP_ID, 1, 'True', 'False') from Employee

CASE Function:

案例功能

SELECT CASE WHEN EMP_ID = 1 THEN 'True' ELSE 'False' END from Employee

回答by Rob van Wijk

Two other alternatives:

另外两种选择:

  1. a combination of NULLIFand NVL2. You can only use this if emp_idis NOT NULL, which it is in your case:

    select nvl2(nullif(emp_id,1),'False','True') from employee;
    
  2. simple CASEexpression(Mt. Schneiders used a so-called searched CASEexpression)

    select case emp_id when 1 then 'True' else 'False' end from employee;
    
  1. 的组合NULLIFNVL2。您只能使用 if emp_idis NOT NULL,这就是您的情况:

    select nvl2(nullif(emp_id,1),'False','True') from employee;
    
  2. 简单CASE表达式(Mt. Schneiders 使用了所谓的搜索CASE表达式)

    select case emp_id when 1 then 'True' else 'False' end from employee;
    

回答by Kaushik Nayak

In PL/SQL, there is a trick to use the undocumented OWA_UTIL.ITEfunction.

在 PL/SQL 中,有一个使用未记录OWA_UTIL.ITE函数的技巧。

SET SERVEROUTPUT ON

DECLARE
    x   VARCHAR2(10);
BEGIN
    x := owa_util.ite('a' = 'b','T','F');
    dbms_output.put_line(x);
END;
/

F

PL/SQL procedure successfully completed.