1.流程控制
【if…then…elsif…then…else…end if】
--查询出150号员工的工资,若其工资大于或等于1000则打印'salary>=1000';
--若在5000到10000之间,则打印'5000<=salary<10000';否则打印'salary<5000'
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
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_emp varchar2(30);
begin
select salary into v_sal from employees where employee_id = 150;
if v_sal>= 10000
then v_emp := 'salary >= 10000';
elsif v_sal >= 5000
then v_emp := '5000 <= salary <10000';
else v_emp := 'salary < 5000';
end if;
dbms_output.put_line(v_emp);
end;
【case…when…then…when…then…else…end】
--查询出150号员工的工资,若其工资大于或等于1000则打印'salary>=1000';
--若在5000到10000之间,则打印'5000<=salary<10000';否则打印'salary<5000'
declare
v_sal employees.salary%type;
v_emp varchar2(30);
begin
select salary into v_sal from employees where employee_id = 150;
v_emp :=
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_sal || ',' ||v_emp);
end;
2.流程循环
【loop…exit when…end loop】
--输出1-100
--①初始化条件②循环体③循环条件④迭代条件
declare
--①
v_i number(5) := 1;
begin
loop
--②
dbms_output.put_line(v_i);
--③
exit when v_i >= 100;
--④
v_i := v_i+1;
end loop;
end;
【for…in…loop…end loop】
自动判断循环条件
begin
for c in 1..100 loop
dbms_output.put_line(c);
end loop;
end;
【while…loop…end loop】
declare
--①
v_i number(5) := 1;
begin
while v_i <= 100 loop
dbms_output.put_line(v_i);
v_i := v_i+1;
end loop;
end;