• [bbk4982]第09集 Chapter 05 Writing Control Structures(01)


    Logic Tables

    Build a simple Boolean condition with a comparision operator.

    AND TRUE FALSE NULL
    TRUE TRUE FALSE NULL
    FALSE FALSE FALSE FALSE
    NULL NULL FALSE NULL

          

    OR TRUE FALSE NULL
    TRUE TRUE TRUE TRUE
    FALSE TRUE FALSE NULL
    NULL TRUE NULL NULL
    NOT  
    TRUE FALSE
    FALSE TRUE
    NULL NULL

    Boolean Expression or Logincal Expression 

    What is the value of flag in each case?

    flag := record_flag AND available_flag;

    RECORD_FLAG AVAILABLE_FLAG FLAG
    TRUE TRUE ?(1)
    TRUE FALSE ?(2)
    NULL TRUE ?(3)
    NULL FALSE ?(4)

    Short-Circuit Evaluation

    PL/SQL uses short-circuit evaluation,which means that PL/SQL need not evaluate all of the expression in an IF statements. 

    如果运算符号是AND,FALSE具有黑洞特色;

    如果运算符号是OR,TRUE具有黑洞特色;

    IF condition1 AND condition2
    THEN
        statements;
    ELSE
        statements;
    END IF;
    IF condition1 OR condition2
    THEN
        statements;
    ELSE
        statements;
    END IF;

    Demo01:如果condition1计算结果是FALSE,那么condition2就不用计算.

    Demo02:如果condition1计算结果是TRUE,那么condition2就不用计算.

    Quiz

    Is the following statement short-circuit when condition1 is NULL,when condition1 is FALSE?

    my_boolean := condtion1 AND condition2;

    The NULL Statement

    NULL除了可以作为一个表达式之外,还可以作为一个Statement.

    NULL expression;

    NULL Statement;

    Syntax:

    --NULL Statement;
    NULL;

    When you want PL/SQL to do absolutely nothing,you can use the NULL statements.The reasons to the use NULL statement are often the two following scenarios:

    • Improving program readability.
    • Using the NULL statement after a label.(oracle 语法规定,标签必须在语句之前.)
      • DECLARE
        ...
        BEGIN
            IF condition THEN GOTO lastpoint END IF;
            ...
            ...
            ...
        <<lastpoint>>
            NULL;
        END;
        /

    Iterative Control:Loop Statements

    • Loops repeat a statement(or a sequence of statements) multiple times.
    • There are three loop types:
      • Basic loop
      • FOR loop
      • WHILE loop

    Basic Loops

    Syntax:

    LOOP
        statement1;
        ...
        ...
        ...
        EXIT [WHEN condition];
    END LOOP;

    Basic Loops中如果没有EXIT ,就将会是一个死循环.

    DECLARE
            v_countryid     loc.country_id%TYPE := 'CA';
            v_loc_id        loc.location_id%TYPE;
            v_counter       NUMBER(2) := 1;
            v_new_city      loc.city%TYPE := 'Montreal';
    BEGIN
            SELECT MAX(location_id) INTO v_loc_id   FROM loc WHERE country_id = v_countryid;
            LOOP
                    INSERT INTO loc(location_id,city,country_id)
                    VALUES((v_loc_id + v_counter),v_new_city,v_countryid);
    
                    v_counter := v_counter + 1;
    
                    EXIT WHEN v_counter > 3;
    
            END LOOP;
    
            COMMIT;
    EXCEPTION
            WHEN OTHERS THEN
                    ROLLBACK;
                    DBMS_OUTPUT.PUT_LINE('Error Occured;');
    END;
    
    /
    
    SELECT location_id,city,country_id FROM loc;

    WHILE LOOPS

    Syntax:

    WHILE condition LOOP
        statement1;
        statement2;
    END LOOP;

    Use the WHILE loop to repeat statements while a condition is TRUE.

    DECLARE
            v_countryid     loc.country_id%TYPE := 'CA';
            v_loc_id        loc.location_id%TYPE;
            v_counter       NUMBER(2) := 1;
            v_new_city      loc.city%TYPE := 'alta';
    BEGIN
            SELECT MAX(location_id) INTO v_loc_id   FROM loc WHERE country_id = v_countryid;
    
            /*
            LOOP
                    INSERT INTO loc(location_id,city,country_id) VALUES((v_loc_id + v_counter),v_new_city,v_countryid);
    
                    v_counter := v_counter + 1;
    
                    EXIT WHEN v_counter > 3;
    
            END LOOP;
            */
    
            WHILE v_counter < 4
            LOOP
                    INSERT INTO loc(location_id,city,country_id) VALUES((v_loc_id + v_counter),v_new_city,v_countryid);
                    v_counter := v_counter + 1;
    END LOOP;
    
            COMMIT;
    EXCEPTION
            WHEN OTHERS THEN
                    ROLLBACK;
                    DBMS_OUTPUT.PUT_LINE('Error Occured;');
    END;
    
    /
    
    SELECT location_id,city,country_id FROM loc;

    FOR Loops(PL/SQL中默认的步长是1,不可修改)

    Use a For loop to shortcut the test for the number of iterations.

    Do not declare the counter;it is declared implicity.

    FOR counter IN [REVERSE] lower_bound..upper_bound 
    LOOP
        statement1;
        statement2;
        ...
    END LOOP;
    DEMO:REVERSE
    BEGIN
            FOR i IN REVERSE 1..10
            LOOP
                    DBMS_OUTPUT.PUT_LINE(i);
            END LOOP;
    END;
    
    /
    DECLARE
            v_countryid     loc.country_id%TYPE := 'CA';
            v_loc_id        loc.location_id%TYPE;
            v_counter       NUMBER(2) := 1;
            v_new_city      loc.city%TYPE := 'Montreal';
    BEGIN
            SELECT MAX(location_id) INTO v_loc_id   FROM loc WHERE country_id = v_countryid;
            /*
            LOOP
                    INSERT INTO loc(location_id,city,country_id)
                    VALUES((v_loc_id + v_counter),v_new_city,v_countryid);
    
                    v_counter := v_counter + 1;
    
                    EXIT WHEN v_counter > 3;
    
            END LOOP;
            */
    
            FOR i IN 1..3
            LOOP
                    INSERT INTO loc(location_id,city,country_id) VALUES((v_loc_id + v_counter),v_new_city,v_countryid);
    
                    v_counter := v_counter + 1;
    
            END LOOP;
    
    
            COMMIT;
    EXCEPTION
            WHEN OTHERS THEN
                    ROLLBACK;
                    DBMS_OUTPUT.PUT_LINE('Error Occured;');
    END;
    
    /
    
    SELECT location_id,city,country_id FROM loc;
  • 相关阅读:
    假期小作业1
    Python_day4
    Python_day3
    Python_day2
    12/06
    12/05
    python系统学习:第三周之简单的三级菜单
    python系统学习:第二周之字典应用
    python系统学习:第二周之字符串函数练习
    python系统学习:第二周之购物车功能
  • 原文地址:https://www.cnblogs.com/arcer/p/3038173.html
Copyright © 2020-2023  润新知