读书人

pl/sql的if.elsif备忘

发布时间: 2014-04-23 16:13:46 作者: rapoo

pl/sql的if..elsif备忘
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;

??? 如下所示:

???
pl/sql的if.elsif备忘
??? 使用elsif写感觉简洁多了,elsif的语法如下:

???

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

??? 全文完。

?

读书人网 >SQL Server

热点推荐