天天看點

PL/SQL基本程式結構和語句1.條件結構2.循環結構3.順序結構

1.條件結構

CASE

文法

CASE selector
        WHEN 'value1' THEN S1;
        WHEN 'value2' THEN S2;
        WHEN 'value3' THEN S3;
        ...
        ELSE Sn;  -- default case
    END CASE;
           

執行個體

DECLARE
   grade char() := 'A';
BEGIN
   CASE grade
      when 'A' then dbms_output.put_line('Excellent');
      when 'B' then dbms_output.put_line('Very good');
      when 'C' then dbms_output.put_line('Well done');
      when 'D' then dbms_output.put_line('You passed');
      when 'F' then dbms_output.put_line('Better try again');
      else dbms_output.put_line('No such grade');
   END CASE;
END;
           

IF

文法

  1. IF THEN 結構
    IF condition THEN 
           S;
        END IF;
               
  2. IF THEN ELSIF ELSE 文法
    IF(boolean_expression 1)THEN 
       S1; -- Executes when the boolean expression 1 is true 
    ELSIF( boolean_expression 2) THEN
       S2;  -- Executes when the boolean expression 2 is true 
    ELSIF( boolean_expression 3) THEN
       S3; -- Executes when the boolean expression 3 is true 
    ELSE 
       S4; -- executes when the none of the above condition is true 
    END IF;
               
  3. IF THEN ELSE 文法
    IF condition THEN
       S1; 
    ELSE 
       S2;
    END IF;
               

執行個體

  1. IF THEN 結構執行個體
DECLARE
   a number() := ;
BEGIN
   a:= ;
  -- check the boolean condition using if statement 
   IF( a <  ) THEN
      -- if condition is true then print the following  
      dbms_output.put_line('a is less than 20 ' );
   END IF;
   dbms_output.put_line('value of a is : ' || a);
END;
/
           
  1. IF THEN ELSIF ELSE 結構執行個體
DECLARE
   a number(3) := 100;
BEGIN
   -- check the boolean condition using if statement 
   IF( a <  ) THEN
      -- if condition is true then print the following  
      dbms_output.put_line('a is less than 20 ' );
   ELSE
      dbms_output.put_line('a is not less than 20 ' );
   END IF;
   dbms_output.put_line('value of a is : ' || a);
END;
/
           
  1. IF THEN ELSE 結構執行個體
DECLARE
   a number(3) := 100;
BEGIN
   -- check the boolean condition using if statement 
   IF( a <  ) THEN
      -- if condition is true then print the following  
      dbms_output.put_line('a is less than 20 ' );
   ELSE
      dbms_output.put_line('a is not less than 20 ' );
   END IF;
   dbms_output.put_line('value of a is : ' || a);
END;
/
           

2.循環結構

LOOP

文法

LOOP
  Sequence of statements;
END LOOP;
           

執行個體

DECLARE
   x number := ;
BEGIN
   LOOP
      dbms_output.put_line(x);
      x := x + ;
      IF x >  THEN
         exit;
      END IF;
   END LOOP;
   -- after exit, control resumes here
   dbms_output.put_line('After Exit x is: ' || x);
END;
/
           

FOR

文法

FOR counter IN initial_value .. final_value LOOP
   sequence_of_statements;
END LOOP;
           

執行個體

DECLARE
   a number(2);
BEGIN
   FOR a in  ..  LOOP
       dbms_output.put_line('value of a: ' || a);
  END LOOP;
END;
/
           

3) WHILE

文法

WHILE condition LOOP
   sequence_of_statements
END LOOP;
           

執行個體

DECLARE
   a number() := ;
BEGIN
   WHILE a <  LOOP
      dbms_output.put_line('value of a: ' || a);
      a := a + ;
   END LOOP;
END;
/
           

3.順序結構

GOTO

在PL/SQL程式設計語言的GOTO語句提供無條件跳轉到在同一個子程式的GOTO标簽的語句。

注意:GOTO語句是不建議使用在任何程式設計語言,因為它使得程式難以跟蹤控制流程,使程式難以了解,難以修改。如果使用GOTO的任何程式可以改寫,就盡量不要使用GOTO語句。

文法

GOTO label;
..
..
<< label >>
statement;
           

執行個體

DECLARE
   a number() := ;
BEGIN
   <<loopstart>>
   -- while loop execution 
   WHILE a <  LOOP
      dbms_output.put_line ('value of a: ' || a);
      a := a + ;
      IF a =  THEN
         a := a + ;
         GOTO loopstart;
      END IF;
   END LOOP;
END;
/
           

NULL

NULL隻是把控制權傳遞給後面的語句。

NULL語句的用處:

n 為GOTO語句提供一個目标

n 通過使條件語句的含義和行為更加清晰來提高可讀性

n 建立占位符和子程式樁

n 表明清楚這種可能性,但不需要進行處理。