条件控制
if形式:
if 条件表达式 then
语句段
end if;
if…else形式:
if 条件表达式 then
语句段1
else
语句段2
end if;
多重if…else形式:
if 条件表达式1 then
语句段1
elsif 条件表达式2 then
语句段2
elsif 条件表达式3 then
语句段3
......
elsif 条件表达式n then
语句段n
end if;
和我们常用的编程语言中的条件语句类似,只有一点需要注意就是elsif
示例:
员工奖金发放:
- 输入员工编号
- 如果该员工本来没有奖金,则奖金按照工资的10%算
- 有奖金但不超过1000,补到1000
- 超过1000的,把奖金再提高10%
declare
v_emp emp%rowtype;
begin
v_emp.empno:=&eno;
select * into v_emp from emp where empno=v_emp.empno;
dbms_output.put_line('原奖金:'||nvl(v_emp.comm,0));
if v_emp.comm is null then
update emp set comm=v_emp.sal*0.1 where empno=v_emp.empno;
elsif v_emp.comm<1000 then
update emp set comm=1000 where empno=v_emp.empno;
elsif v_emp.comm>1000 then
update emp set comm=v_emp.comm*1.1 where empno=v_emp.empno;
end if;
select * into v_emp from emp where empno=v_emp.empno;
dbms_output.put_line('现奖金为:'||v_emp.comm);
exception
when no_data_found then
dbms_output.put_line('没有找到您输入的员工编号');
end;
case形式:
单点类型:
declare
v_deptno dept.deptno%type:=&deptno;
begin
case v_deptno
when 10 then dbms_output.put_line('部门所在地:纽约');
when 20 then dbms_output.put_line('部门所在地:达拉斯');
when 30 then dbms_output.put_line('部门所在地:芝加哥');
when 40 then dbms_output.put_line('部门所在地:波士顿');
else dbms_output.put_line('不存在该部门');
end case;
end;
点范围类型:
输入员工编号,输出员工工资级别
- sal<2000 A级
- sal>2000 and sal<3000 B级
- 其他 C级
declare
v_empno emp.empno%type:=&eno;
v_sal emp.sal%type;
begin
select sal into v_sal from emp where empno=v_empno;
case
when v_sal<2000 then dbms_output.put_line('A级工资');
when v_sal>=2000 and v_sal<3000 then dbms_output.put_line('B级工资');
else dbms_output.put_line('C级工资');
end case;
exception
when no_data_found then
dbms_output.put_line('没有找到该员工');
end;
case主要还是用于单点控制,点范围其实就和if…else没区别了。
循环控制
基本循环
LOOP
语句段:
EXIT [WHEN条件表达式]
END LOOP;
上述语法中,当使用基本循环时,无论是否满足条件,语句至少会被执行一次。当条件表达式为TRUE时,会退出循环,并执行END LOOP后的操作。
Exit语句必须放在循环体内,且只能退出循环体,不能退出PL/SQL块。
当需要退出PL/SQL块时,应该使用RETURN语句。
示例:
(把table的内容循环添加到dept表中)
declare
type dept_table_type is table of dept%rowtype
index by binary_integer;
i number(1):=0; --定义循环变量
v_dept_table dept_table_type;
begin
v_dept_table(0).deptno:=50;
v_dept_table(0).dname:='研发部';
v_dept_table(0).loc:='北京';
v_dept_table(1).deptno:=60;
v_dept_table(1).dname:='测试部';
v_dept_table(1).loc:='上海';
v_dept_table(2).deptno:=70;
v_dept_table(2).dname:='实施部';
v_dept_table(2).loc:='杭州';
loop
if i>2 then
exit;
end if;
insert into dept values(v_dept_table(i).deptno,v_dept_table(i).dname,v_dept_table(i).loc);
i:=i+1;
end loop;
end;
while循环
基本循环至少需要执行一次循环体内的语句,而WHILE循环中,只有当条件为TRUE时,才会执行循环体内的语句。
while [条件表达式] loop
循环内容段;
end loop;
示例:
declare
type dept_table_type is table of dept%rowtype
index by binary_integer;
i number(1):=0; --定义循环变量
v_dept_table dept_table_type;
begin
v_dept_table(0).deptno:=50;
v_dept_table(0).dname:='研发部';
v_dept_table(0).loc:='北京';
v_dept_table(1).deptno:=60;
v_dept_table(1).dname:='测试部';
v_dept_table(1).loc:='上海';
v_dept_table(2).deptno:=70;
v_dept_table(2).dname:='实施部';
v_dept_table(2).loc:='杭州';
while i<=2 loop
insert into dept values(v_dept_table(i).deptno,v_dept_table(i).dname,v_dept_table(i).loc);
i:=i+1;
end loop;
end;
for循环
当使用基本循环或WHILE循环时,需要定义循环控制变量。循环控制变量不仅可以使用NUMBER类型,还可以使用其他数据类型。当使用FOR循环时,Oracle会隐含定义循环变量。
默认情况下,当使用FOR循环时,每次循环时控制变量会自动增加l;如果指定了REVERSE选项,则每次循环时循环控制变量会自动减少1。在循环体内部可以引用循环变量,但不能对其赋值。
declare
type dept_table_type is table of dept%rowtype
index by binary_integer;
v_dept_table dept_table_type;
begin
v_dept_table(0).deptno:=50;
v_dept_table(0).dname:='研发部';
v_dept_table(0).loc:='北京';
v_dept_table(1).deptno:=60;
v_dept_table(1).dname:='测试部';
v_dept_table(1).loc:='上海';
v_dept_table(2).deptno:=70;
v_dept_table(2).dname:='实施部';
v_dept_table(2).loc:='杭州';
for i in 0..v_dept_table.count-1 loop
dbms_output.put_line(v_dept_table(i).deptno||'+'||v_dept_table(i).dname||'+'||v_dept_table(i).loc);
end loop;
end;