您的位置:首页 > 数据库 > Oracle

Oracle控制结构详解

2012-11-01 14:08 399 查看
------------------------------------
if 条件表达式 then ..
------------------------------------
-- 编写一个过程,可以输入一个雇员名,如果该雇员的工资低于2000,就给雇员工资增加10%
create or replace procedure sp_pro(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*1.1 where ename=spName;
end if;
end;

--调用过程
exec sp_pro('SCOTT');

------------------------------------
if 条件表达式 then .. else ..
--------------------------------------------
-- 编写一个过程,可以输入一个雇员名,如果该雇员的补助不是0,就在原基础上增加100,如果补助为0,就把补助设为200
create or replace procedure sp_pro(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;

--调用过程
exec sp_pro('SCOTT');

----------------------------------------------------
if 条件表达式 then .. elsif 条件表达式 then .. else ..
----------------------------------------------------
-- 编写一个过程,可以输入一个雇员编号,
--如果该雇员的职位是PRESIDENT就给他的工资增加1000,
--如果该雇员的职位是MANAGER就给他的工资增加500,
--其他职位的雇员工资增加200
create or replace procedure sp_pro(spNo number) is
--定义
v_job emp.job%type;
begin
--执行
select job into v_job from emp where empno=spNo;
--判断
if v_job='PREDIENT' 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_pro(7839);

------------------------------
while
------------------------------
--语法
while 条件表达式 loop
执行语句...;
end loop;

--案例
--循环向表插入数据
create or replace table test_1(
empno number primary key,
ename varchar2(20) default '' not null
);

create or replace procedure pro(v_in_ename varchar2, v_in_n number) is
--定义变量
v_empno number:=200;
begin
delete test_1;
while v_empno<=200+v_in_n loop --while循环
insert into test_1 values(v_empno, v_in_ename);
v_empno:=v_empno+1;
end loop;
end;

------------------------------
for
------------------------------
--语法
for i in reverse 1..10 loop
执行语句;
end loop;

--案例
create or replace procedure pro(v_in_ename varchar2, v_in_n number) is
--定义变量
v_empno number:=200;
begin
delete test_1;
for i in reverse 1..10 loop --for循环
insert into test_1 values(v_empno, v_in_ename);
v_empno:=v_empno+1;
end loop;
end;

------------------------------
goto,null
------------------------------
1、goto跳转到特定标号去执行

--案例
declare
i number:=1;
begin
<<start_loop>>
loop
dbms_output.put_line('输出i='||i);
if i=12 then
goto end_loop;
end if;
i:=i+1;
if i=10 then
goto start_loop;
end if;
end loop;
<<end_loop>>
dbms_output.put_line('循环结束');
end;

2、null 语句不会执行任何操作

--案例
declare
v_sal emp.sal%type;
v_ename emp.ename%type;
begin
select ename,sal into v_ename,v_sal from emp where empno=&no;
if v_sal<3000 then
update emp set comm=sal*0.1 where ename=v_ename;
else
null; --null 其实没有任何意义
end if;
end;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: