PL/SQL中的流程控制语句

一、条件控制语句
1、只有IF的条件控制语句
IF concition THEN
STATEMENTS;
END IF;
请看以下示例:
DECLARE

        salaryAVG number(7,2);

        empSalary number(7,2);

        BEGIN

        SELECT AVG(SAL) INTO salaryAVG FROM scott.emp;

        SELECT SAL INTO empSalary FROM scott.emp WHERE EMPNO=&eno;

        IF empSalary>salaryAVG THEN

        DBMS_OUTPUT.PUT_LINE('该职工工资超过了平均线'||salaryAVG);

        END IF;

        END;

        


2、IF、ELSE语句
IF condition THEN
STATEMENTS;
ELSE
STATEMENTS;
END IF;
请看以下示例:
DECLARE

        salaryAVG number(7,2);

        empSalary number(7,2);

        BEGIN

        SELECT AVG(SAL) INTO salaryAVG FROM scott.emp;

        SELECT SAL INTO empSalary FROM scott.emp WHERE EMPNO=&eno;

        IF empSalary>salaryAVG THEN

        DBMS_OUTPUT.PUT_LINE('该职工工资超过了平均线'||salaryAVG);

        ELSE

        DBMS_OUTPUT.PUT_LINE('该职工工资没有超过平均线'||salaryAVG);

        END IF;

        END;

        


3、IF ESLEIF ELSE语句
IF condition THEN
STATEMENTS;
ELSIF 
condition THEN 
STATEMENTS;
ELSE
STATEMENTS;
END IF;
请看以下示例:
DECLARE

        empSalary number(7,2);

        BEGIN

        SELECT SAL INTO empSalary FROM scott.emp WHERE EMPNO=&eno;

        IF empSalary<2000 THEN

        DBMS_OUTPUT.PUT_LINE('需交税0元');

        ELSIF empSalary<4000 THEN

        DBMS_OUTPUT.PUT_LINE('需交税'||(empSalary*0.15-175)||'元');

        ELSE

        DBMS_OUTPUT.PUT_LINE('需交税'||(empSalary*0.25-375)||'元');

        END IF;

        END;

        


4、CASE语句
单一值进行比较:
CASE selector
WHEN expression1 THEN result1;
WHEN expression2 THEN result2;
WHEN expression3 THEN result3;
ELSE result4;
END CASE;
请看以下示例:
DECLARE

        eno NUMBER(2);

        BEGIN

        eno:=&no;

        CASE eno

        WHEN 10 THEN DBMS_OUTPUT.PUT_LINE('部门1');

        WHEN 20 THEN DBMS_OUTPUT.PUT_LINE('部门2');

        WHEN 30 THEN DBMS_OUTPUT.PUT_LINE('部门3');

        ELSE DBMS_OUTPUT.PUT_LINE('没有该部门');

        END CASE;

        END;

        --多重条件进行比较:

        CASE

        WHEN expression1 THEN result1;

        WHEN expression2 THEN result2;

        WHEN expression3 THEN result3;

        ELSE result4;

        END CASE;

        


二、循环控制语句
1、LOOP循环
LOOP
STATEMENTS;
END LOOP
;
示例1:
DECLARE

        a int;

        BEGIN

        a:=0;

        LOOP

        IF a=10 THEN

        EXIT;

        END IF;

        DBMS_OUTPUT.PUT_LINE(a);

        a:=a+1;

        END LOOP;

        END;

        


示例2:
DECLARE

        a int;

        BEGIN

        a:=0;

        LOOP

        EXIT WHEN a=10;

        DBMS_OUTPUT.PUT_LINE(a);

        a:=a+1;

        END LOOP;

        END;

        


2、WHILE循环

示例1:
DECLARE

        a int;

        BEGIN

        a:=0;

        WHILE a<10 LOOP

        DBMS_OUTPUT.PUT_LINE(a);

        a:=a+1;

        END LOOP;

        END;

        


3、FOR循环

示例1:
BEGIN

        FOR a IN 0..9 LOOP

        DBMS_OUTPUT.PUT_LINE(a);

        END LOOP;

        END;

        


4、多重循环与标签
示例1:
DECLARE

        result INT;

        BEGIN

        <
        
          
         
           > FOR i IN 1..10 LOOP <
         
            
            > FOR j IN 1..10 LOOP result:=i*j; EXIT outer WHEN result=10; EXIT WHEN result=5; DBMS_OUTPUT.PUT_LINE('内:'||result); END LOOP inner; DBMS_OUTPUT.PUT_LINE('外:'||result); END LOOP outer; DBMS_OUTPUT.PUT_LINE('最后:'||result); END;  
           
        
          


三、顺序控制语句
1、GOTO语句
语法:GOTO labelName;
2、NULL语句
NULL;语句不执行任何操作,直接传递到下一条语句。

你可能感兴趣的:(pl/sql)