pl/sql学习8——流程控制语句

本文通过一个具体的PL/SQL程序示例介绍了如何使用if语句、case语句、loop循环、while循环及for循环来实现不同的业务逻辑。示例中详细展示了条件判断和循环的使用方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

declare
  v_index integer;
  v_name  dept.dname%type;
  cursor c_dept is
    select dname from dept;
begin
  dbms_output.put_line('if语句控制:');
  v_index := 12;
  if v_index < -1 then
    dbms_output.put_line('v_inex<-1:' || v_index);
  elsif v_index >= -1 and v_index < 10 then
    dbms_output.put_line('v_inex>-1 and v_index<10:' || v_index);
    elsif v_index>=10 and v_index<=100 then
          null;
  else
    dbms_output.put_line('v_inex>100:' || v_index);
  end if;

  dbms_output.put_line('case语句控制:');

  case
    when v_index < -1 then
      dbms_output.put_line('v_inex<-1:' || v_index);
    when v_index < 20 then
      dbms_output.put_line('v_inex<20:' || v_index);
    when v_index < 50 then
      dbms_output.put_line('v_inex<50:' || v_index);
    else
      dbms_output.put_line('v_inex>=50:' || v_index);
    
  end case;
  dbms_output.put_line('loop循环');
  open c_dept;
  loop
    fetch c_dept
      into v_name;
    dbms_output.put_line('loop部门名:' || v_name);
    exit when c_dept%notfound;
  end loop;
  close c_dept;

  dbms_output.put_line('while循环');

  while v_index > 1 loop
    dbms_output.put_line('while:v_index值:' || v_index);
    v_index := v_index - 1;
    continue when v_index = 5;
    exit when v_index = 2;
  end loop;

  dbms_output.put_line('for循环');
  for iten in c_dept loop
    dbms_output.put_line('for部门名:' || v_name);
  end loop;
end;


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值