且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

自学PL/SQL 第四讲Writing Control Structures

更新时间:2022-09-20 17:21:31

本讲内容包括:
介绍控制语句的类型和用法
IF语句的结构
CASE表达式
介绍不同的循环语句
使用嵌套循环和标签

一:在PL/SQL中可以使用IF和LOOP进行分支判断和嵌套循环
IF语句的结构主要有:
– IF-THEN-END IF
– IF-THEN-ELSE-END IF
– IF-THEN-ELSIF-END IF

IF语句的语法:


  1. IF condition THEN  
  2.    statements;  
  3. [ELSIF condition THEN  
  4.    statements;]  
  5. [ELSE  
  6.    statements;]  
  7. END IF; 

自学PL/SQL 第四讲Writing Control Structures

condition是布尔型的变量(TRUE,FALSE,NULL)和表达式,只有条件返回为TRUE的时候后面的语句才会执行:
condition is a Boolean variable or expression (TRUE, FALSE,or NULL). (It is associated with a sequence of statements, which is executed only if the expression yields TRUE.)

THEN is a clause that associates the Boolean expression that precedes it with the sequence of statements that follows it. statements can be one or more PL/SQL or SQL statements. (They may include further IF statements containing several nested IF, ELSE,and ELSIF statements.)

ELSIF is a keyword that introduces a Boolean expression. (If the first condition yields FALSE or NULL then the ELSIF keyword introduces additional conditions.)
ELSE is a keyword that executes the sequence of statements that follows it if the control reaches it.

复合型的IF条件语句,可以使用AND和OR操作符


  1. IF v_ename = 'Vargas' AND salary > 6500 THEN  
  2. v_deptno :60;  
  3. END IF; 

可以嵌套IF条件语句


  1. IF condition1 THEN  
  2. statement1;  
  3. ELSE  
  4. IF condition2 THEN  
  5. statement2;  
  6. END IF;  
  7. END IF;  
  8.  
  9. IF condition1 THEN  
  10. statement1;  
  11. ELSIF condition2 THEN  
  12. statement2;  
  13. ELSIF condition3 THEN  
  14. statement3;  
  15. END IF; 

 二:CASE表达式,可以使用CASE表达式,匹配并返回结果


  1. CASE selector  
  2. WHEN expression1 THEN result1  
  3. WHEN expression2 THEN result2  
  4. ...  
  5. WHEN expressionN THEN resultN  
  6. [ELSE resultN+1;]  
  7. END;  
  8.  
  9. SQL> set serveroutput on  
  10. SQL> set verify off  
  11. SQL> define p_1=c 
  12. DECLARE  
  13.    v_1 CHAR(1) :UPPER('&p_1');  
  14.    v_2 VARCHAR2(20);  
  15. BEGIN  
  16.     v_2 :=  
  17. CASE v_1  
  18.    WHEN 'A' THEN 'ONE'  
  19.    WHEN 'B' THEN 'TWO'  
  20.    WHEN 'C' THEN 'THREE'  
  21.    ELSE 'NOTHING'  
  22. END;  
  23.    DBMS_OUTPUT.PUT_LINE ( v_2 || '  map ' || v_1);  
  24. END;  
  25.  
  26. SQL> /  
  27. THREE  map C  
  28. PL/SQL 过程已成功完成 

三:处理空值
任何值(包括空值)和空值比较返回空值,如果IF语句的条件返回为空,则后面的分支不会执行

自学PL/SQL 第四讲Writing Control Structures

 四:loop循环;loop循环分basic,for,while三种


  1. basic循环语法:  
  2. LOOP                             -- delimiter  
  3. statement1;                    -- statements  
  4. ...  
  5. EXIT [WHEN condition];  -- EXIT statement  
  6. END LOOP;                    -- delimiter  
  7.  
  8. condition is a Boolean variable or  
  9. expression (TRUE, FALSE, or NULL);  
  10.  
  11. for循环语法:  
  12. FOR counter IN [REVERSE]  
  13. lower_bound..upper_bound LOOP  
  14. statement1;  
  15. statement2;  
  16. ...  
  17. END LOOP;  
  18.  
  19. DECLARE  
  20. v_lower NUMBER :1;  
  21. v_upper NUMBER :100 
  22. BEGIN  
  23. FOR i IN v_lower..v_upper LOOP  
  24. ...  
  25. END LOOP;  
  26. END; 

循环指导原则:
Use the basic loop when the statements inside the loop must execute at least once.
Use the WHILE loop if the condition has to be evaluated at the start of each iteration.
Use a FOR loop if the number of iterations is known.

五:嵌套循环,循环可以被无限嵌套,在不同的循环块上使用标签,使用exit退出循环


  1. BEGIN  
  2. <<Outer_loop>> 
  3. LOOP  
  4.    v_counter :v_counter+1;  
  5.    EXIT WHEN v_counter>10;  
  6.    <<Inner_loop>> 
  7.    LOOP  
  8.    ...  
  9.   EXIT Outer_loop WHEN total_done = 'YES';  
  10.    -- Leave both loops  
  11.   EXIT WHEN inner_done = 'YES';  
  12.   -- Leave inner loop only  
  13.   ...  
  14.   END LOOP Inner_loop;  
  15.   ...  
  16. END LOOP Outer_loop;  
  17. END; 

 本文转自斩月博客51CTO博客,原文链接http://blog.51cto.com/ylw6006/656775如需转载请自行联系原作者


ylw6006