首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

pl/sql的if.elsif备忘

2014-04-23 
pl/sql的if..elsif备忘declarev_id numberbeginv_id : &idif v_id 10 thendbms_output.put_line(the

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;

??? 全文完。

?

热点排行