1 IF条件语句
1.1 IF条件语句格式一
IF<布尔表达式>THEN
PL/SQL 和 SQL 语句;
END IF;
1.2 IF条件语句格式二
IF<布尔表达式>THEN
PL/SQL 和 SQL 语句;
ELSE
其它语句;
END IF;
1.3 IF条件语句格式三
IF<布尔表达式>THEN
PL/SQL 和 SQL 语句;
ELSLF<其它布尔表达式>THEN
其它语句;
ELSLF<其它布尔表达式>THEN
其它语句;
ELSE
其它语句;
END IF;
提示:ELSIF不能写成ELSELF
例如:
declare
v_sal employees.salary%type;
begin
select salary into v_sal
from employees
where employee_id = 150;
if v_sal >= 10000 then
dbms_output.put_line('salary >= 10000');
--elsif v_sal >= 5000 and v_sal <10000 then
elsif v_sal >= 5000 then
dbms_output.put_line('5000 <= salary <10000');
else
dbms_output.put_line('salary <5000');
end if;
end;
declare
v_sal employees.salary%type;
v_temp varchar2(20);
begin
select salary into v_sal from employees where employee_id = 150;
if v_sal >= 10000 then v_temp := 'salary >= 10000';
elsif v_sal >= 5000 then v_temp := '5000 <= salary <10000';
else v_temp := 'salary <5000';
end if;
dbms_output.put_line(v_temp);
end;
2 CASE表达式
CASE selector
WHEN expression1 THEN result1
WHEN expression2 THEN result2
WHEN expressionN THEN resultN
[ELSE resultN+1]
END;
例如:
declare
v_sal employees.salary%type;
v_temp varchar2(20);
begin
select salary into v_sal from employees where employee_id = 150;
v_temp :=
case trunc(v_sal/5000) when 0 then 'salary < 5000'
when 1 then '5000 <= salary< 10000'
else 'salary >= 10000'
end;
dbms_output.put_line(v_temp);
end;