1、条件语句 if else判断
#声明变量
declare employee_sa number;
begin
select count(*) into employee_sa from employees where salary>6000;
if employee_sa=1 then
dbms_output.put_line('本公司有一名薪资大于6000的员工');
elsif employee_sa>1 then
dbms_output.put_line('本公司有多名薪资大于6000的员工');
esle
dbms_output.put_line('本公司没有薪资大于6000的员工');
end if;
end;
2、case when 分支判断
#声明变量
declare employee_sa number;
begin
select count(*) into employee_sa from employees where salary>6000;
case when employee_sa=0 then
dbms_output.put_line('本公司没有薪资大于6000的员工');
when employee_sa=1 then
dbms_output.put_line('本公司有一名薪资大于6000的员工');
else
dbms_output.put_line('本公司有多名薪资大于6000的员工');
end case;
end;
3、无条件循环
#无条件循环
-- loop
-- 循环操作
-- end loop
#利用无条件循环输出员工employee_id 处于100~106 之间的所有员工姓名
declare e_id number:=100;
declare e_name varchar2(20);
begin
loop
if e_id>=106 then
exit;
end if;
e_id:=e_id+1;
select first_name into e_name from employees where employee_id=e_id;
dbms_output.put_line(e_id|| '号员工是:'|| e_name);
end loop;
end;
4、while 循环
#语法
-- while 条件判断 loop
-- 循环操作
-- end loop;
declare e_id number:=100;
declare e_name varchar2(20);
begin
while e_id<106 loop
e_id:=e_id+1;
select first_name into e_name from employees where employee_id=e_id;
dbms_output.put_line(e_id|| '号员工是:'|| e_name);
end loop;
end;
5、for 循环
#语法(注意:or 循环自行声明变量,因此无需对e_id 声明)
-- for counter
-- in lower_bound upper_bound
-- loop
-- 循环操作
-- end loop;
declare e_name varchar2(20);
begin
for e_id in 100..106 loop
select first_name into e_name from employees where employee_id=e_id;
dbms_output.put_line(e_id|| '号员工是:'|| e_name);
end loop;
end;