PL/SQL流程式控制制語句

來源:互聯網
上載者:User

PL/SQL流程式控制制語句

PL/SQL流程式控制制語句

介紹PL/SQL的流程式控制制語句, 包括如下三類:
    控制語句: IF 語句
    迴圈語句: LOOP語句, EXIT語句
    順序語句: GOTO語句, NULL語句
①if語句
IF <布林運算式> THEN
    PL/SQL 和SQL語句;
ELSIF< 其它布林運算式> THEN
    其它語句;
ELSIF< 其它布林運算式> THEN
    其它語句;
ELSE
    其它語句;
END IF;

例:
declare
    v_emp_name employees.last_name%type;
    v_emp_sal employees.salary%type;
    v_emp_sal_level varchar2(20);
begin
    select last_name,salary into v_emp_name,v_emp_sal from employees where employee_id = 150;
   
    if(v_emp_sal >= 10000) then v_emp_sal_level := 'salary >= 10000';
    elsif(v_emp_sal >= 5000) then v_emp_sal_level := '5000<= salary < 10000';
    else v_emp_sal_level := 'salary < 5000';
    end if;
   
    dbms_output.put_line(v_emp_name||','||v_emp_sal||','||v_emp_sal_level);
end;

② CASE 運算式
    CASE selector
        WHEN expression1 THEN result1
        WHEN expression2 THEN result2
        WHEN expressionN THEN resultN
        [ ELSE resultN+1]
    END;

例:
declare
      v_sal employees.salary%type;
      v_msg varchar2(50);
begin   
      select salary into v_sal
      from employees
      where employee_id = 150;
     
      --case 不能向下面這樣用
      /*
      case v_sal when salary >= 10000 then v_msg := '>=10000'
                  when salary >= 5000 then v_msg := '5000<= salary < 10000'
                  else v_msg := 'salary < 5000'
      end;
      */
 
      v_msg :=
            case trunc(v_sal / 5000)
                  when 0 then 'salary < 5000'
                  when 1 then '5000<= salary < 10000'
                  else 'salary >= 10000'
            end;
     
      dbms_output.put_line(v_sal ||','||v_msg);
end;

③迴圈
1.  簡單迴圈
    LOOP
        要執行的語句;
        EXIT WHEN<條件陳述式> ; /*條件滿足,退出迴圈語句*/
    END LOOP;

2.  WHILE 迴圈(相較1,推薦使用2)
    WHILE<布林運算式> LOOP
        要執行的語句;
    END LOOP;
3.數字式迴圈
    FOR迴圈計數器IN[REVERSE ] 下限.. 上限LOOP
        要執行的語句;
    END LOOP;
每迴圈一次,迴圈變數自動加1;使用關鍵字REVERSE,迴圈變數自動減1。
跟在IN REVERSE 後面的數字必須是從小到大的順序,而且必須是整數,不能是變數或運算式。可以使用EXIT退出迴圈。

例:使用迴圈語句列印 1 - 100.(三種方式)

1).  LOOP ... EXIT WHEN ... END LOOP
declare
      --初始化條件
      v_i number(3) := 1;
begin
      loop
      --迴圈體
        dbms_output.put_line(v_i);
 --迴圈條件
        exit when v_i = 100;
 --迭代條件
        v_i := v_i + 1;
      end loop;
end;

2). WHILE ... LOOP ... END LOOP
declare
      --初始化條件
      v_i number(3) := 1;
begin
      --迴圈條件
      while v_i <= 100 loop
      --迴圈體
            dbms_output.put_line(v_i);
      --迭代條件
            v_i := v_i + 1;
      end loop;
end;

3).
begin
      for i in 1 .. 100 loop
            dbms_output.put_line(i);
      end loop;
end;

例: 綜合使用 if, while 語句, 列印 1 - 100 之間的所有素數
(素數: 有且僅用兩個正約數的整數, 2, 3, 5, 7, 11, 13, ...).

declare
    v_i int := 2;
    v_j int := 2;
    v_flag boolean := false;
begin
    while v_i < 100 loop       
        v_j := 2;
        while  v_j<sqrt(v_i) loop
            if mod(v_i,v_j) = 0 then
              v_flag := true;
              exit;
            end if;
          v_j := v_j + 1;
        end loop;       
        if v_flag = false then
          dbms_output.put_line(v_i||',是素數');
        end if;     
        v_flag := false; 
        v_i := v_i +1;   
    end loop; 
end;

④標號和GOTO
PL/SQL中GOTO語句是無條件跳轉到指定的標號去的意思。文法如下:
GOTO  label;
. . .  . . .
<<label>> /*標號是用<<>>括起來的標識符*/

例:列印1——100的自然數,當列印到50時,跳出迴圈,輸出“列印結束”
(方法一)
begin
  for i in 1..100 loop
      dbms_output.put_line(i);
      if(i = 50) then
          goto label;
      end if;
  end loop;
     
  <<label>>
  dbms_output.put_line('列印結束');

end;
(方法二)
begin
  for i in 1..100 loop
      dbms_output.put_line(i);
      if(i mod 50 = 0) then
          dbms_output.put_line('列印結束');
          exit;
      end if;
  end loop;
end;

Oracle--plsql複合資料型別

--------------------------------------分割線 --------------------------------------

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

SQLPLUS spool 到動態記錄檔名

Oracle SQLPLUS提示符設定

通過設定SQLPLUS ARRAYSIZE(行預取)加快SQL返回速度

PL/SQL Developer實用技巧分享

--------------------------------------分割線 --------------------------------------

相關文章

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.