控制结构
- 介绍
在任何计算机语言(C,java,pascal)都有各种控制语句
(条件语句,循环结构,顺序控制结构)
在pl/sql中也存在这样的控制结构
条件分支语句
pl/sql提供了三种条件分支语句
- if --then
- if --then --else
- if --then --elsif – else
简单的条件判断
if – then
--编写一个过程,可以输入一个雇员名,如果该雇员工资低于2000,就给该雇员工资增加10%
create procedure sp_pro2(spname varchar2) is
--定义
v_sal emp.sal%type;
begin
--执行
select sal into v_sal from emp where ename = spname;
--判断
if v_sal < 2000 then
update emp set sal=sal+sal*10% where ename = spname;
end if;
end;
/
exec sp_pro6('scott');
二重条件分支
if – then – else
/*编写一个过程,可以输入一个雇员名
如果该雇员的补助不是0,就在原来的基础上增加100;
如果补助为0就把补助设为200;
*/
create procedure sp_pro3(spname varchar2) is
v_comm emp.comm%type;
begin
select comm into v_comm from emp
where ename = spname;
--判断
if v_comm < 0 then
update emp set comm = comm+100 where ename=spname;
else
update emp set comm = comm+200 where ename=spname;
end if;
end;
/
多重条件分支
if – then – elsif – else
/*编写一个过程,可以输入一个员工编号
如果该雇员的职位是president,就给他的工资增加1000;
如果该雇员的职位是manager,就给他的工资增加500;
其它职位的雇员工资增加200*/
create procedure sp_pro4(spno number) is
v_job emp.job%type;
begin
select job into v_job from emp
where empno = spno;
if v_job = 'president' then
update emp set sal=sal+1000 where empno=spno;
elsif v_job = 'manager' then
update emp set sal=sal+500 where empno=spno;
else
update emp set sal=sal+200 where empno=spno;
end if;
end;
/
exec sp_pro4(7839); --7839为员工编号