pl/sql的if..elsif备忘

     最近在试着重新看下pl/sql,印象中pl/sql的if语句后面是可以跟elseif的。写了个简单的判断数字大小的语句:

     

declare
  v_id number;
begin
  v_id := &id;
  if v_id < 10 then
    dbms_output.put_line('the value is:' || v_id ||
                         ' it''s true that the value less than 10 ');
    elseif v_id < 20 then dbms_output.put_line('the value is:' || v_id ||
                                               ' it''s true that the value is less than20 ');
    elseif v_id < 30 then dbms_output.put_line('the value is:' || v_id ||
                                               ' it''s true that the value is  less than 30 ');
  end if;
end;

    在pl/sql下面elseif不变色,试着运行,直接报错了,从报错信息上看,在elseif附近存在错误,第一感觉是pl/sql 下elseif不是关键字,而是把elseif拆成else if,这样就ok了

   

set serveroutput on;
declare
  v_id number;
begin
  v_id := &id;
  if v_id < 10 then
    dbms_output.put_line('the value is:' || v_id ||
                         ' it''s true that the value is less than 10 ');
  else
    if v_id < 20 then
      dbms_output.put_line('the value is:' || v_id ||
                           ' it''s true that the value is less than 20 ');
    else
      if v_id < 30 then
        dbms_output.put_line('the value is:' || v_id ||
                             ' it''s true that the value is less than 30 ');
      else
        dbms_output.put_line('the value is:' || v_id ||
                             ' it''s true that the value is greater than 30 ');
      end if;
    end if;
  end if;
end;

    这样写要特别注意end if和if的对应关系,一不小心漏掉一个end if就错了,有没有简单点的写法呢?
    其实pl/sql是支持else if这样的语法的,不过不是elseif,而是elsif,else少了一个e,用elsif写就是下面这样:

   

set serveroutput on;
declare
  v_id number;
begin
  v_id := &id;
  if v_id < 10 then
    dbms_output.put_line('the value is:' || v_id || ' it''s true that the value is less than 10 ');
  elsif v_id < 20 then
    dbms_output.put_line('the value is:' || v_id || ' it''s true that the value is less than 20 ');
  elsif v_id < 30 then
    dbms_output.put_line('the value is:' || v_id || ' it''s true that the value is less than 30 ');
  else
    dbms_output.put_line('the value is:' || v_id || ' it''s true that the value is greater than 30 '); 
end if;
end;

    我上面的是在cmd window下运行的,如果想直接在sql window下按F8运行,请去掉:

   

set serveroutput on;

    如下所示:

   

    使用elsif写感觉简洁多了,elsif的语法如下:

   

IF THEN
  --do something...
ELSIF THEN
  --do something...
ELSIF THEN
  --do something...
ELSE
  --do something...
END IF;

    全文完。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值