PL/SQL有3种类型的条件控制语句:
▪IF语句
▪ELSIF语句
▪CASE语句
1.IF语句
▪if-then语句
语法格式:
if condition then
statement;
end if;
▪if-then-else语句
语法格式:
if condition then
statement1;
else
statement2;
end if;
例:输入一个数,判断它的奇偶性
SQL> declare
2 num number:=3;
3 begin
4 if mod(num,2)<>0 then
5 dbms_output.put_line(num||'是奇数');
6 else
7 dbms_output.put_line(num||'是偶数');
8 end if;
9 end;
10 /
注:mod(num1,num2)取num1除于num2的余数
2.ELSIF语句
语法格式:
if condition1 then
statement1;
elsif condition2 then
statement2;
end;
3.CASE语句
▪CASE语句:
语法格式:
case selector
when expression1 then result1;
when expression2 then result2;
...
when expressionN then resultN;
else resultN+1;
end case;
▪搜索式表达式:
语法格式:
case
when condition1 then statement1;
when condition2 then statement2;
...
when conditionN then statementN;
else resultN+1;
end case;