2016年2月10日 星期三

PL/SQL interview question 001


Uri Lukach 提供了以下的 PL/SQL interview question[1]:


答案是 C.

使用 case-when expression 時有兩種方式, 一為 simple-case-expression, 另一為 searched-case-expression. 使用 searched-case-express 時, 若在 condition 中沒有使用 relational operator 便會產生 ORA-00920 invalid relational operator 的例外[2]

但是, 我們不能將問題中的 condition 改成 v_input = TRUE, 因為此 case-when expression 是在 SELECT SQL statement 中使用,SQL engine 中沒有 boolean data type (只有在 pl/sql 中才有 boolean data type)。

以下正確執行的版本供參考:
 set serveroutput on  
 DECLARE  
 v_input boolean :=true;  
 v_result varchar2(10);  
 BEGIN  
 v_result := case   
    when v_input=true then 'TRUE'  
    when (v_input <>true) then 'FALSE'  
    else null  
    end;  
 DBMS_OUTPUT.PUT_LINE(v_result);  
 END;  
 /  

沒有留言: