PL/SQL流程控制

本文深入解析SQL中的if判断、ifelse判断、ifelsifelse判断,以及循环(loop、while、for)的基本用法和注意事项。通过实例演示如何在数据库操作中灵活运用这些控制结构。

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

  • if判断
    declare
           v_b boolean:=true;
    begin if v_b then
             dbms_output.put_line('ok');
          end if;
    end;
  • if else判断
    declare
            v_b boolean:=true;
    begin
         if v_b then
            dbms_output.put_line('ok');
         else
            dbms_output.put_line('false');
         end if;
    end;
  • if elsif else判断
    declare
            v_name varchar2(20):='cheng';
    begin
         if v_name='0701' then
            dbms_output.put_line('0701');
         elsif v_name='cheng' then
            dbms_output.put_line('cheng');
         else
            dbms_output.put_line('false');
         end if;
    end;
  • loop循环,注意推出exit是推出循环,而不是推出整个代码块
    declare
           v_i binary_integer:=0;
    begin
         loop
             if v_i>10 then
                exit;
             end if;
             v_i:=v_i+1;
             dbms_output.put_line('hehe');
         end loop;
             dbms_output.put_line('over');
    end;

            loop简化写法
       declare
         v_i binary_integer :=0;
       begin
       loop
         exit when v_i>10;
         v_i :=v_i+1;
         dbms_output.put_line('hehe');
     end loop;
         dbms_output.put_line('over');
        end;

  • while循环
    declare
           v_i binary_integer:=0;
    begin
           while v_i<10 loop
                 dbms_output.put_line('hello'||v_i );
                 v_i:=v_i+1;
           end loop;
           dbms_output.put_line('over');
    end;
  • for循环,注意不需要声明变量
    begin
         for v_i in 0..10 loop
             dbms_output.put_line('hello'||v_i);
         end loop;
             dbms_output.put_line('over');
    end;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值