对于一个即将有成就的DBA来说,不懂PLSQL,真的有些说不过去,本人下定决心开始玩PLSQL了,

教材:Oracle官方文档 Oracle® Database PL/SQL User's Guide and Reference  10g Release 2 (

时间:15--30天

比起C,java来说PLSQL是简单多了,本人先做一个实验来说明控制结构

1.Conditional Control

Background:Often, it is necessary to take alternative actions depending on circumstances. The
IF-THEN-ELSE statement lets you execute a sequence of statements conditionally.
The IF clause checks a condition, the THEN clause defines what to do if the condition
is true and the ELSE clause defines what to do if the condition is false or null.

Example:shows the use of IF-THEN-ELSE to determine the salary raise an employee receives based on the current salary of the employee.To choose among several values or courses of action, you can use CASE constructs. The CASE expression evaluates a condition and returns a value for each case. The case statement evaluates a condition and performs an action, such as an entire PL/SQL block, for each case.

  1. DECLARE 
  2.    jobid      employees.job_id%TYPE; 
  3.    empid      employees.employee_id%TYPE :115
  4.    sal        employees.salary%TYPE; 
  5.    sal_raise  NUMBER(3,2); 
  6. BEGIN 
  7.   SELECT job_id, salary INTO jobid, sal from employees WHERE employee_id = empid
  8.   CASE 
  9.     WHEN jobid = 'PU_CLERK' THEN 
  10.         IF sal < 3000 THEN sal_raise := .12; 
  11.           ELSE sal_raise := .09; 
  12.         END IF; 
  13.     WHEN jobid = 'SH_CLERK' THEN 
  14.         IF sal < 4000 THEN sal_raise := .11; 
  15.           ELSE sal_raise := .08; 
  16.         END IF; 
  17.     WHEN jobid = 'ST_CLERK' THEN 
  18.         IF sal < 3500 THEN sal_raise := .10; 
  19.           ELSE sal_raise := .07; 
  20.         END IF; 
  21.     ELSE 
  22.      BEGIN 
  23.        DBMS_OUTPUT.PUT_LINE('No raise for this job: ' || jobid); 
  24.      END; 
  25.   END CASE; 
  26.   UPDATE employees SET salarysalary = salary + salary * sal_raise  
  27.     WHERE employee_id = empid
  28.   COMMIT; 
  29. END; 

 2.Iterative Control

Background:LOOP statements let you execute a sequence of statements multiple times. You place the keyword LOOP before the first statement in the sequence and the keywords END LOOP after the last statement in the sequence. The following example shows the simplest kind of loop, which repeats a sequence of statements continually:
        LOOP
        -- sequence of statements
        END LOOP;
The FOR-LOOP statement lets you specify a range of integers, then execute a sequence
of statements once for each integer in the range.

Example: Loop inserts 100 numbers, square roots, squares, and the sum of squares into a database table:

  1. CREATE TABLE sqr_root_sum (num NUMBER, sq_root NUMBER(6,2), 
  2.                            sqr NUMBER, sum_sqrs NUMBER); 
  3. DECLARE 
  4.    s PLS_INTEGER; 
  5. BEGIN 
  6.   FOR i in 1..100 LOOP 
  7.     s := (i * (i + 1) * (2*i +1)) / 6; -- sum of squares 
  8.     INSERT INTO sqr_root_sum VALUES (i, SQRT(i), i*i, s ); 
  9.   END LOOP; 
  10. END; 

Background:The WHILE-LOOP statement associates a condition with a sequence of statements. Before each iteration of the loop, the condition is evaluated. If the condition is true, the
sequence of statements is executed, then control resumes at the top of the loop. If the condition is false or null, the loop is bypassed and control passes to the next statement.

Example: you find the first employee who has a salary over $15000 and is higher in the chain of command than employee 120:

  1. CREATE TABLE temp (tempid NUMBER(6), tempsal NUMBER(8,2), tempname VARCHAR2(25)); 
  2. DECLARE 
  3.    sal            employees.salary%TYPE :0
  4.    mgr_id         employees.manager_id%TYPE; 
  5.    lname          employees.last_name%TYPE; 
  6.    starting_empid employees.employee_id%TYPE :120
  7. BEGIN 
  8.    SELECT manager_id INTO mgr_id FROM employees  
  9.       WHERE employee_id = starting_empid
  10.    WHILE sal <= 15000 LOOP -- loop until sal > 15000 
  11.       SELECT salary, manager_id, last_name INTO sal, mgr_id, lname 
  12.          FROM employees WHERE employee_id = mgr_id
  13.    END LOOP; 
  14.    INSERT INTO temp VALUES (NULL, sal, lname); -- insert NULL for tempid 
  15.    COMMIT; 
  16. EXCEPTION 
  17.    WHEN NO_DATA_FOUND THEN 
  18.       INSERT INTO temp VALUES (NULL, NULL, 'Not found'); -- insert NULLs 
  19.       COMMIT; 
  20. END; 

3.Sequential Control

Background:The GOTO statement lets you branch to a label unconditionally. The label, an
undeclared identifier enclosed by double angle brackets, must precede an executable statement or a PL/SQL block. When executed, the GOTO statement transfers control to the labeled statement or block

Example:

  1. DECLARE 
  2.   total   NUMBER(9) :0
  3.   counter NUMBER(6) :0
  4. BEGIN 
  5.   <<calc_total>> 
  6.     counter :counter + 1; 
  7.     total :total + counter * counter; 
  8.     -- branch to print_total label when condition is true 
  9.     IF total > 25000 THEN GOTO print_total; 
  10.       ELSE GOTO calc_total; 
  11.     END IF; 
  12.   <<print_total>> 
  13.   DBMS_OUTPUT.PUT_LINE('Counter: ' || TO_CHAR(counter) || ' Total: ' ||  
  14. TO_CHAR(total)); 
  15. END;