Oracle PL/SQL基礎 選擇(IF CASE) 、 迴圈(LOOP WHILE FOR)

來源:互聯網
上載者:User

一、PL/SQL的控制結構,包括選擇結構、迴圈結構和跳轉結構

rlwrap - 解決Linux下SQLPLUS退格、上翻鍵亂碼問題

SQLPLUS spool 到動態記錄檔名

Oracle SQLPLUS提示符設定

通過設定SQLPLUS ARRAYSIZE(行預取)加快SQL返回速度
 
1、選擇結構
 
(1)IF語句

IF condition1 THEN  statement1 ;
[ ELSIF condition2 THEN statement2 ; ]
...
[ ELSE else_statements ; ]
END IF;

Attention:PL/SQL的邏輯運算結果有 TRUE 、 FALSE 、NULL 三種情況,所以在進行條件選擇判斷時,要考慮條件結果為NULL的情況。

Example:

create or replace procedure detector_plsql_if(
      v_control_type in varchar2)
as
    v_pre_print varchar2(100) := 'value of parameter [in] v_control_type';
begin

    if v_control_type = 'if' then
      dbms_output.put_line(v_pre_print || ' (if) :' || v_control_type);
    elsif v_control_type = 'case' then
      dbms_output.put_line(v_pre_print || ' (case) :' || v_control_type);
    elsif v_control_type is null then
      dbms_output.put_line(v_pre_print || ' (null) :' || v_control_type);
    else
      dbms_output.put_line(v_pre_print || ' :' || v_control_type);
    end if;

end detector_plsql_if;

(2)、CASE 語句

CASE語句有兩種形式:一種只進行等值比較,另一種可以進行多各種條件比較。

A、只進行等值比較

CASE test_value
 WHEN value1 THEN statement1;
[ WHEN value2 THEN statement2; ]
...
[ ELSE else_statement ; ]
END CASE;

Example:

create or replace procedure detector_plsql_case(
  v_control_type in varchar2)
is
  v_pre_print varchar2(100) := 'value of parameter [in] v_control_type';
begin

    case v_control_type
      when 'if' then
          dbms_output.put_line(v_pre_print || ' (if) :' || v_control_type);
      when 'case' then
          dbms_output.put_line(v_pre_print || ' (case) :' || v_control_type);
      else
          dbms_output.put_line(v_pre_print || ' :' || v_control_type);
    end case;

end detector_plsql_case;

B、進行多種條件的比較

CASE
        WHEN condition1 THEN statement1 ;
        [ WHEN condition2 THEN statement2; ]
        ...
        [ ELSE else_statements ; ]
END CASE;

CASE語句對每一個WHEN條件進行判斷,當條件為真時,執行其後面的語句;如果所有條件都不為真,則執行ELSE後的語句。

Attention:在CASE語句中,當第一個WHEN條件為真時,執行其後的操作,操作完成後結束CASE語句,其它的WHEN條件不再判斷,其後的操作也不執行。

Example:

create or replace procedure detector_plsql_case2(
 v_control_type in varchar2)
as
  v_pre_print varchar2(100) := 'value of parameter [in] v_control_type';
begin

  case
    when v_control_type = 'if' then
        dbms_output.put_line(v_pre_print || ' (if) :' || v_control_type);
    when v_control_type = 'case' then
          dbms_output.put_line(v_pre_print || ' (case) :' || v_control_type);
    when v_control_type is null then
          dbms_output.put_line(v_pre_print || ' (null) :' || v_control_type);
    else
          dbms_output.put_line(v_pre_print || ' :' || v_control_type);     
  end case;

end detector_plsql_case2;

更多詳情見請繼續閱讀下一頁的精彩內容:

  • 1
  • 2
  • 下一頁

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.