oracle流程控制语法

流程控制结构
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;
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值