1. 程式人生 > >oracle中if/else的3種寫法

oracle中if/else的3種寫法

1、標準sql規範

複製程式碼
 1 一、單個IF
 2 1 3 
 4 if a=...  then
 5 .........
 6 end if;
 7 
 8 2 9 
10 if a=... then
11 ......
12 else
13 ....
14 end if;
15 
16 二、多個IF
17 
18 if a=..  then
19 ......
20 elsif a=..  then
21 ....
22 end if;     
23 這裡中間是“ELSIF”,而不是ELSE IF 。這裡需要特別注意
複製程式碼

2、decode函式

DECODE的語法

DECODE(value,if1,then1,if2,then2,if3,then3,...,else)

表示如果value等於if1時,DECODE函式的結果返回then1,...,如果不等於任何一個if值,則返回else。

3、case when

case when a='1'then 'xxxx'
     when a='2' then 'ssss'
else
  'zzzzz'
end as

注意點: 

1、以CASE開頭,以END結尾 
2、分支中WHEN 後跟條件,THEN為顯示結果 
3、ELSE 為除此之外的預設情況,類似於高階語言程式中switch case的default,可以不加 
4、END 後跟別名