流程控制结构
1、if条件结构 if...end if;
--接收两个数,判断两个数的大小关系
declare
a number;
b number;
begin
a:='&a';
b:='&b';
if(a=b) then
dbms_output.put_line('a=b');
elsif(a>b) then
dbms_output.put_line('a>b');
else
dbms_output.put_line('a<b');
end if;
end;
2、case条件结构
1)用于查询
select deptno,
case deptno
when 10 then 'A'
when 20 then 'B'
when 30 then 'C'
end 部门 from emp;
2)用于PL/SQL块
--根据接收的分数,评出相应的等级
declare
score varchar2(2);
grade varchar2(10);
begin
score:='&score';
case upper(score)
when 'A' then grade:='优秀';
when 'B' then grade:='良好';
when 'C' then grade:='及格';
when 'D' then grade:='不及格';
else grade:='非法成绩';
end case;
dbms_output.put_line(grade);
end;
3、循环结构 loop...end loop;
1)无限循环(死循环),使用exit退出循环
declare
num number:=0;
begin
loop
num:=num+1;
dbms_output.put_line(num||'-hello');
/*if(num=10) then
dbms_output.put_line('任务完成了');
exit;
end if;*/
exit when num=10;
end loop;
end;
2)while循环(初、判、变)
declare
num number:=0;--初
begin
while(num<10)--判
loop
num:=num+1;--变
dbms_output.put_line(num||'-hello');
end loop;
end;
3)for循环(循环固定次数)
begin
for num in 1..10
loop
if(num mod 2=0) then
dbms_output.put_line(num||'-hello');
end if;
end loop;
end;
注意:
a- num 不用声明,且不能修改但可用于表达式
b- .. 表示区间运算符,下限..上限
c- reverse 表示反向输出
begin
for num in reverse 1..10
loop
if(num mod 2=0) then
dbms_output.put_line(num||'-hello');
end if;
end loop;
end;
4)嵌套循环
--输出 5*10 的星号
begin
for h in 1..5
loop
for l in 1..10
loop
dbms_output.put('*');
end loop;
dbms_output.new_line();
end loop;
end;
![](https://i-blog.csdnimg.cn/blog_migrate/5c36cecacd0d7008c496aa4dae939df2.png)