PL/SQL --> 流程控制

--=======================

--  PL/SQL --> 流程控制

--=======================

 

    类似于高级语言,流程控制语句是PL/SQL语言的重要组成部分。这些流程控制语句使得PL/SQL加大了代码的灵活性和多样性,大大简化了

程序的编写。下面将列出流程控制语句并给出具体事例。

   

一、顺序结构

    按代码的书写顺序依次执行

    --在下面的示例中将逐步执行代码

        scott@ORCL> declare v_ename varchar2(20);

          2  v_job emp.job%type;

          3  v_no  emp.empno%type;

          4  begin

          5    v_no:=&inputno;

          6    select ename,job into v_ename,v_job from emp where empno=v_no;

          7    dbms_output.put_line('Employee Name: '||v_ename);

          8    dbms_output.put_line('Employee Job : '||v_job);

          9  end;

         10  /

        Enter value for inputno: 7788

        old   5:   v_no:=&inputno;

        new   5:   v_no:=7788;

        Employee Name: SCOTT

        Employee Job : ANALYST

 

        PL/SQL procedure successfully completed.   

二、条件分支结构

    1.IF ... THEN ... END IF

        IF condition THEN

            statement

        END IF;

 

        判断condition是否成立,成立就执行IF END IF 之间的语句。

   

        --例:输入员工编号,查询其工资,如果他们的职位是CLERK,则工资增加%,再显示修改前后的工资数。

 

            DECLARE

                v_empid emp.empno%TYPE;

                v_job emp.job%TYPE;

                v_old_sal emp.sal%TYPE;

                v_new_sal emp.sal%TYPE;

            BEGIN

                v_empid:=&inputid;

                SELECT job,sal INTO v_job,v_old_sal FROM emp WHERE empno=v_empid;

                IF v_job='CLERK' THEN

                    v_new_sal:=v_old_sal*1.1;

                    UPDATE emp SET sal=v_new_sal WHERE empno=v_empid;

                    DBMS_OUTPUT.PUT_LINE('Old sal:'||v_old_sal);

                    DBMS_OUTPUT.PUT_LINE('New sal:'||v_new_sal);

                END IF;

            EXCEPTION

                WHEN NO_DATA_FOUND THEN

                    DBMS_OUTPUT.PUT_LINE('Not FOUND RECORD');

            END;

            /

 

    2.IF ... THEN ... ELSE ... END IF

        IF condition THEN

            statements1;

        ELSE

            statements2;

        END IF;

 

        判断condition是否成立,成立就执行IF ELSE 之间的语句,

        否则执行ELSE END IF之间的语句。

 

        --例:输入员工编号,查询其工资,如果他们的职位是CLERK,则工资增加%,如果不是CLERK,工资增加8%,再显示修改前后的工资数。

 

            DECLARE

                v_empid emp.empno%TYPE;

                v_job emp.job%TYPE;

                v_old_sal emp.sal%TYPE;

                v_new_sal emp.sal%TYPE;

            BEGIN

                v_empid:=&inputid;

                SELECT job,sal INTO v_job,v_old_sal FROM emp WHERE empno=v_empid;

                IF v_job='CLERK' THEN

                    v_new_sal:=v_old_sal*1.1;

                ELSE

                    v_new_sal:=v_old_sal*1.08;

                END IF;

 

                UPDATE emp SET sal=v_new_sal WHERE empno=v_empid;

                DBMS_OUTPUT.PUT_LINE('Old sal:'||v_old_sal);

                DBMS_OUTPUT.PUT_LINE('New sal:'||v_new_sal);

 

            EXCEPTION

                WHEN NO_DATA_FOUND THEN

                    DBMS_OUTPUT.PUT_LINE('Not FOUND RECORD');

            END;

            /

 

    3.IF ... THEN ... ELSIF ... THEN ... ELSE ... END IF

        IF condition1 THEN

            statements1;

        ELSIF condition2 THEN

            statements2;

        ELSE

            else_statements;

        END IF;

 

        --:输入员工编号,查询其工资,如果其职位是CLERK,则工资增加%,如果是SALESMAN工资增加%,其它的加%,显示修改前后的工资数。

 

            DECLARE

                v_empid emp.empno%TYPE;

                v_job emp.job%TYPE;

                v_old_sal emp.sal%TYPE;

                v_new_sal emp.sal%TYPE;

            BEGIN

                v_empid:=&inputid;

                SELECT job,sal INTO v_job,v_old_sal FROM emp WHERE empno=v_empid;

                IF v_job='CLERK' THEN

                    v_new_sal:=v_old_sal*1.1;

                ELSIF v_job='SALESMAN' THEN

                    v_new_sal:=v_old_sal*1.08;

                ELSE

                    v_new_sal:=v_old_sal*1.05;

                END IF;

 

                UPDATE emp SET sal=v_new_sal WHERE empno=v_empid;

                DBMS_OUTPUT.PUT_LINE('Old sal:'||v_old_sal);

                DBMS_OUTPUT.PUT_LINE('New sal:'||v_new_sal);

 

            EXCEPTION

                WHEN NO_DATA_FOUND THEN

                    DBMS_OUTPUT.PUT_LINE('Not FOUND RECORD');

            END;

            /

       

    4.等值比较的CASE多分支

       

        CASE expression

            WHEN result_1 THEN

            statements1;

            WHEN result_2 THEN

            statemnts2;

              ......

            [ELSE

            else_statements;]

        END CASE;

       

        --使用case分支完成前面的示例

        --:输入员工编号,查询其工资,如果其职位是CLERK,则工资增加%,如果是SALESMAN工资增加%,其它的加%,显示修改前后的工资数。

            DECLARE

                v_empid emp.empno%TYPE;

                v_job emp.job%TYPE;

                v_old_sal emp.sal%TYPE;

                v_new_sal emp.sal%TYPE;

            BEGIN

                v_empid:=&inputid;

                SELECT job,sal INTO v_job,v_old_sal FROM emp WHERE empno=v_empid;

                CASE v_job

                    WHEN 'CLERK' THEN

                    v_new_sal:=v_old_sal*1.1;

                    WHEN 'SALESMAN' THEN

                    v_new_sal:=v_old_sal*1.08;

                    ELSE

                    v_new_sal:=v_old_sal*1.05;

                END CASE;

 

                UPDATE emp SET sal=v_new_sal WHERE empno=v_empid;

                DBMS_OUTPUT.PUT_LINE('Old sal:'||v_old_sal);

                DBMS_OUTPUT.PUT_LINE('New sal:'||v_new_sal);

 

            EXCEPTION

                WHEN NO_DATA_FOUND THEN

                    DBMS_OUTPUT.PUT_LINE('Not FOUND RECORD');

            END;

            /

 

    5.条件比较的CASE语句

        CASE

            WHEN expression_1 THEN

                statements1;

            WHEN expression_2 THEN

                statements2;

            WHEN expression_3 THEN

                statements3;

                ......

            [ELSE

                else_statements]

        END CASE

       

        --使用case分支完成前面的示例,仅仅列出case 部分

            CASE

                WHEN v_job='CLERK' THEN

                v_new_sal:=v_old_sal*1.1;

                WHEN v_job='SALESMAN' THEN

                v_new_sal:=v_old_sal*1.08;

                ELSE

                v_new_sal:=v_old_sal*1.05;

            END CASE;

 

三、循环结构

    1.LOOP 循环

       

        LOOP

            statement;

            EXIT [WHEN condition];

        END LOOP;

 

        --例:用LOOP写一个程序求1++++. . . +10 之和

 

            DECLARE

                v_n INT:=1;

                v_s INT:=0;

            BEGIN

                LOOP

                    v_s:=v_s+v_n;

                    v_n:=v_n+1;

                    EXIT WHEN v_n>10;

                END LOOP;  

                DBMS_OUTPUT.PUT_LINE('1+2+3+...+10='||v_s);

            END;

            /

 

    2.WHILE 循环

   

        WHIEL condition LOOP

            statement;

        END LOOP;

 

        --下面使用while循环完成loop循环中的示例

           

            DECLARE

                v_s NUMBER:=0;

                v_n NUMBER:=1;

            BEGIN

                WHILE v_n<=10 LOOP

                    v_s:=v_s+v_n;

                    v_n:=v_n+1;

                END LOOP;

                DBMS_OUTPUT.PUT_LINE('1+2+3+...+10='||v_s);

            END;

            /

 

    3.FOR循环

       

        FOR loop_index IN [reverse] lowest_number ..highest_number LOOP

            statements;

        END LOOP;

       

        --下面使用for循环完成loop循环中的示例

            DECLARE

                v_s NUMBER:=0;

            BEGIN

                FOR v_n IN 1 ..10 LOOP

                    v_s:=v_s+v_n;

                    DBMS_OUTPUT.PUT_LINE(v_n);

                END LOOP;

            DBMS_OUTPUT.PUT_LINE('1+2+3+...+10='||v_s);

            END;

            /  

 

            DECLARE

                v_s NUMBER:=0;

            BEGIN

                FOR v_n IN REVERSE 1 ..10 LOOP    --注意reverse是反向循环,即从到

                    v_s:=v_s+v_n;

                    DBMS_OUTPUT.PUT_LINE(v_n);

                END LOOP;

            DBMS_OUTPUT.PUT_LINE('1+2+3+...+10='||v_s);

            END;

            /

 

四、GOTO语句

    GOTO label_name

 

        --下面使用goto语句完成loop循环中的示例

            DECLARE

                v_n NUMBER:=1;

                v_s NUMBER:=0;

            BEGIN

                LOOP

                    v_s:=v_s+v_n;

                    v_n:=v_n+1;

                    IF v_n>10 THEN

                        GOTO out;

                    END IF;

                END LOOP;

                <<out>>

                DBMS_OUTPUT.PUT_LINE('1+2+3+...+10='||v_s);

                END;

                /

 

    使用GOTO语句应注意:

        标号后至少要有一条语句

        PL/SQL块内可以相互跳转,内层块可以跳到外层块,但外层块不能跳到内层块

        不能从某一IF语句外部跳到其内部

        不能从某一循环外跳到其内部

        不能从某一子程序外跳到其内

 

五、NULL

    NULL语句不会执行任何操作,并且会直接将控制传递到下一条语句,使用NULL语句主要是提高程序的可阅读性

        --如下面的示例

            DECLARE

                v_sal emp.sal%TYPE;

                v_ename emp.ename%TYPE;

            BEGIN

                SELECT ename,sal into v_ename,v_sal

                FROM emp WHERE empno=&inputno;

                IF v_sal<3000 then

                    UPDATE emp SET comm=sal*1.2 WHERE ename=v_ename;

                ELSE

                    NULL;

                END IF;

            END;

            /

           

六、更多参考

     

有关SQL请参考

        SQL 基础--> 子查询

        SQL 基础-->多表查询

SQL基础-->分组与分组函数

SQL 基础-->常用函数

SQL 基础--> ROLLUP与CUBE运算符实现数据汇总

SQL基础-->层次化查询(START BY ... CONNECT BY PRIOR)

 

    有关PL/SQL请参考

        PL/SQL --> 语言基础

PL/SQL --> 流程控制

PL/SQL --> 存储过程

PL/SQL --> 函数

PL/SQL --> 游标

PL/SQL -->隐式游标(SQL%FOUND)

PL/SQL --> 异常处理(Exception)

PL/SQL --> PL/SQL记录

PL/SQL --> 包的创建与管理

PL/SQL --> 包重载、初始化

PL/SQL --> DBMS_DDL包的使用

PL/SQL --> DML 触发器

PL/SQL --> INSTEAD OF 触发器

 

 

 

 

 

 

 

 

posted @ 2010-12-08 16:41  生活不是用来挥霍的  阅读(189)  评论(0编辑  收藏  举报