SQL> declare 2 mygrade char(1):='A'; 3 res varchar2(20); 4 begin 5 res := 6 CASE mygrade 7 WHEN 'A' THEN 'The mark is 90-100' 8 WHEN 'B' THEN 'The mark is 80-90' 9 WHEN 'C' THEN 'The mark is 70-80' 10 WHEN 'D' THEN 'The mark is 60-70' 11 WHEN 'E' THEN 'The mark is 0-60' 12 END; 13 dbms_output.put_line(res); 14 end; 15 / The mark is 90-100
PL/SQL procedure successfully completed.
SQL> 2 2* mygrade char(1):='A';
SQL> ch /'A'/'F'/ 2* mygrade char(1):='F'; SQL> l? 1 declare 2 mygrade char(1):='F'; 3 res varchar2(20); 4 begin 5 res := 6 CASE mygrade 7 WHEN 'A' THEN 'The mark is 90-100' 8 WHEN 'B' THEN 'The mark is 80-90' 9 WHEN 'C' THEN 'The mark is 70-80' 10 WHEN 'D' THEN 'The mark is 60-70' 11 WHEN 'E' THEN 'The mark is 0-60' 12 END; 13 dbms_output.put_line(res); 14* end; SQL> /
PL/SQL procedure successfully completed.
SQL> declare 2 mygrade char(1):='F'; 3 res varchar2(20); 4 begin 5 res := 6 CASE mygrade 7 WHEN 'A' THEN 'The mark is 90-100' 8 WHEN 'B' THEN 'The mark is 80-90' 9 WHEN 'C' THEN 'The mark is 70-80' 10 WHEN 'D' THEN 'The mark is 60-70' 11 WHEN 'E' THEN 'The mark is 0-60' 12 ELSE 'The mark is null' 13 END; 14 dbms_output.put_line(res); 15 end; 16 / The mark is null
2017-10-31
case從嚴(yán)格意義上講是if選擇語句,是多條if語句的縮寫形式,本質(zhì)是屬于條件判斷,和循環(huán)流程控制不是一個概念
2017-10-31
語法:
CASE selector?
WHEN exp1 then res1
WHEN exp2 then res2
WHEN exp3 then res3
...
[ELSE resN]
end;
SQL> declare
2 mygrade char(1):='A';
3 res varchar2(20);
4 begin
5 res :=
6 CASE mygrade
7 WHEN 'A' THEN 'The mark is 90-100'
8 WHEN 'B' THEN 'The mark is 80-90'
9 WHEN 'C' THEN 'The mark is 70-80'
10 WHEN 'D' THEN 'The mark is 60-70'
11 WHEN 'E' THEN 'The mark is 0-60'
12 END;
13 dbms_output.put_line(res);
14 end;
15 /
The mark is 90-100
PL/SQL procedure successfully completed.
SQL> 2
2* mygrade char(1):='A';
SQL> ch /'A'/'F'/
2* mygrade char(1):='F';
SQL> l?
1 declare
2 mygrade char(1):='F';
3 res varchar2(20);
4 begin
5 res :=
6 CASE mygrade
7 WHEN 'A' THEN 'The mark is 90-100'
8 WHEN 'B' THEN 'The mark is 80-90'
9 WHEN 'C' THEN 'The mark is 70-80'
10 WHEN 'D' THEN 'The mark is 60-70'
11 WHEN 'E' THEN 'The mark is 0-60'
12 END;
13 dbms_output.put_line(res);
14* end;
SQL> /
PL/SQL procedure successfully completed.
SQL> declare
2 mygrade char(1):='F';
3 res varchar2(20);
4 begin
5 res :=
6 CASE mygrade
7 WHEN 'A' THEN 'The mark is 90-100'
8 WHEN 'B' THEN 'The mark is 80-90'
9 WHEN 'C' THEN 'The mark is 70-80'
10 WHEN 'D' THEN 'The mark is 60-70'
11 WHEN 'E' THEN 'The mark is 0-60'
12 ELSE 'The mark is null'
13 END;
14 dbms_output.put_line(res);
15 end;
16 /
The mark is null
PL/SQL procedure successfully completed.