Sie sind auf Seite 1von 52

Database Programming with

PL/SQL
4-1
Conditional Control: IF Statements

Copyright © 2018, Oracle and/or its affiliates. All rights reserved.


Controlling the Flow of Execution
• You can change the logical flow
of statements within the
PL/SQL block with a number of
control structures.
• This lesson introduces three
types of PL/SQL control
structures:
– Conditional constructs with the IF
statement FOR
LOOP
– CASE expressions
– LOOP control structures WHILE

PLSQL S4L1 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 6
Conditional Control: IF Statements
IF Statement
• The IF statement shown below using "pseudocode"
contains alternative courses of action in a block based
on conditions.
• A condition is an expression with a TRUE or FALSE
value that is used to make a decision.
if the region_id is in (5, 13, 21) Conditions
then print "AMERICAS"

otherwise, if the region_id is in (11, 14, 15)


then print "AFRICA"

otherwise, if the region_id is in (30, 34, 35)


then print "ASIA"

PLSQL S4L1 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 7
Conditional Control: IF Statements
CASE Expressions
• CASE expressions are similar to IF statements in that
they also determine a course of action based on
conditions.
• They are different in that they can be used outside of a
PLSQL block in an SQL statement.
• Consider the following pseudocode example:
if the region_id is
5 then print "AMERICAS"
13 then print "AMERICAS"
21 then print "AMERICAS"
11 then print "AFRICA"
14 then print "AFRICA"
15 then print "AFRICA"
30 then print "ASIA" …

PLSQL S4L1 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 8
Conditional Control: IF Statements
LOOP Control Structures
• Loop control structures are repetition statements that
enable you to execute statements in a PLSQL block
repeatedly.
• Three types of loop control structures are supported by
PL/SQL: BASIC, FOR, and WHILE.

FOR
LOOP

WHILE

PLSQL S4L1 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 9
Conditional Control: IF Statements
IF Statements Structure
• The structure of the PL/SQL IF statement is similar to
the structure of IF statements in other procedural
languages.
• It enables PL/SQL to perform actions selectively based
on conditions.
• Syntax:
IF condition THEN
statements;
[ELSIF condition THEN
statements;]
[ELSE
statements;]
END IF;

PLSQL S4L1 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 11
Conditional Control: IF Statements
IF Statements
• Condition is a Boolean variable or expression that
returns TRUE, FALSE, or NULL.
• THEN introduces a clause that associates the Boolean
expression with the sequence of statements that
follows it.

IF condition THEN
statements;
[ELSIF condition THEN
statements;]
[ELSE
statements;]
END IF;

PLSQL S4L1 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 12
Conditional Control: IF Statements
IF Statements Note
• ELSIF and ELSE are optional in an IF statement. You
can have any number of ELSIF keywords but only one
ELSE keyword in your IF statement.
• END IF marks the end of an IF statement and must
be terminated by a semicolon.
IF condition THEN
statements;
[ELSIF condition THEN
statements;]
[ELSE
statements;]
END IF;

PLSQL S4L1 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 16
Conditional Control: IF Statements
Simple IF Statement
• This is an example of a simple IF statement with a
THEN clause.
• The v_myage variable is initialized to 31.

DECLARE
v_myage NUMBER := 31;
BEGIN
IF v_myage < 11
THEN
DBMS_OUTPUT.PUT_LINE('I am a child');
END IF;
END;

PLSQL S4L1 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 17
Conditional Control: IF Statements
IF THEN ELSE Statement
• The ELSE clause has been added to this example.
• The condition has not changed, thus it still evaluates to
FALSE.

DECLARE
v_myage NUMBER:=31;
BEGIN
IF v_myage < 11
THEN
DBMS_OUTPUT.PUT_LINE('I am a child');
ELSE
DBMS_OUTPUT.PUT_LINE('I am not a child');
END IF;
END;

PLSQL S4L1 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 19
Conditional Control: IF Statements
IF ELSIF ELSE Clause
• The IF DECLARE

statement now
v_myage NUMBER := 31;
BEGIN

contains multiple
IF v_myage < 11
THEN
DBMS_OUTPUT.PUT_LINE('I am a child');
ELSIF clauses as ELSIF v_myage < 20
THEN
well as an ELSE DBMS_OUTPUT.PUT_LINE('I am young');
ELSIF v_myage < 30

clause. THEN
DBMS_OUTPUT.PUT_LINE('I am in my twenties');
ELSIF v_myage < 40
• Notice that the THEN
DBMS_OUTPUT.PUT_LINE('I am in my thirties');

ELSIF clauses ELSE


DBMS_OUTPUT.PUT_LINE('I am mature');

add additional END IF;


END;

conditions.

PLSQL S4L1 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 21
Conditional Control: IF Statements
IF Statement with Multiple Expressions
• An IF statement can have multiple conditional
expressions related with logical operators, such as AND,
OR, and NOT.
• This example uses the AND operator.
• Therefore, it evaluates to TRUE only if both BOTH the
first name and age conditions are evaluated as TRUE.
DECLARE
v_myage NUMBER := 31;
v_myfirstname VARCHAR2(11) := 'Christopher';
BEGIN
IF v_myfirstname ='Christopher' AND v_myage < 11
THEN
DBMS_OUTPUT.PUT_LINE('I am a child named Christopher');
END IF;
END;

PLSQL S4L1 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 25
Conditional Control: IF Statements
NULL Values in IF Statements
• In this example, the v_myage variable is declared but is
not initialized.
• The condition in the IF statement returns NULL, which is
neither TRUE nor FALSE.
• In such a case, the control goes to the ELSE statement
because, just NULL is not TRUE.
DECLARE
v_myage NUMBER;
BEGIN
IF v_myage < 11
THEN
DBMS_OUTPUT.PUT_LINE('I am a child');
ELSE
DBMS_OUTPUT.PUT_LINE('I am not a child');
END IF;
END;

PLSQL S4L1 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 27
Conditional Control: IF Statements
Handling Nulls
When working with nulls, you can avoid some common
mistakes by keeping in mind the following rules:
• Simple comparisons involving nulls always yield NULL.
• Applying the logical operator NOT to a null yields NULL.
• In conditional control statements, if a condition yields
NULL, it behaves just like a FALSE, and the associated
sequence of statements is not executed.

PLSQL S4L1 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 28
Conditional Control: IF Statements
Guidelines for Using IF Statements
Follow these guidelines when using IF statements:
• You can perform actions selectively when a specific
condition is being met.
• When writing code, remember the spelling of the
keywords:
–ELSIF is one word
–END IF is two words

PLSQL S4L1 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 30
Conditional Control: IF Statements
Guidelines for Using IF Statements
• If the controlling Boolean condition is TRUE, then the
associated sequence of statements is executed; if the
controlling Boolean condition is FALSE or NULL, then
the associated sequence of statements is passed over.
• Any number of ELSIF clauses is permitted.
• Indent the conditionally executed
statements for clarity.

PLSQL S4L1 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 31
Conditional Control: IF Statements
Database Programming with
PL/SQL
4-2
Conditional Control: Case Statements

Copyright © 2018, Oracle and/or its affiliates. All rights reserved.


Using a CASE Statement
• Look at this IF statement. What do you notice?
• All the conditions test the same variable v_numvar.
• And the coding is very repetitive: v_numvar is coded
many times.
DECLARE
v_numvar NUMBER;
BEGIN
...
IF v_numvar = 5 THEN statement_1; statement_2;
ELSIF v_numvar = 10 THEN statement_3;
ELSIF v_numvar = 12 THEN statement_4; statement_5;
ELSIF v_numvar = 27 THEN statement_6;
ELSIF v_numvar ... – and so on
ELSE statement_15;
END IF; ...
END;

PLSQL S4L2 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 5
Conditional Control: Case Statements
Using a CASE Statement
• Here is the same logic, but using a CASE statement.
• It is much easier to read. v_numvar is written only
once.
DECLARE
v_numvar NUMBER;
BEGIN
...
CASE v_numvar
WHEN 5 THEN statement_1; statement_2;
WHEN 10 THEN statement_3;
WHEN 12 THEN statement_4; statement_5;
WHEN 27 THEN statement_6;
WHEN ... – and so on
ELSE statement_15;
END CASE;
...
END;

PLSQL S4L2 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 6
Conditional Control: Case Statements
Searched CASE Statements
• You can use CASE statements to test for non-equality
conditions such as <, >, >=, etc.
• These are called searched CASE statements.
• The syntax is virtually identical to an equivalent IF
statement.
DECLARE
v_num NUMBER := 15;
v_txt VARCHAR2(50);
BEGIN
CASE
WHEN v_num > 20 THEN v_txt := 'greater than 20';
WHEN v_num > 15 THEN v_txt := 'greater than 15';
ELSE v_txt := 'less than 16';
END CASE;
DBMS_OUTPUT.PUT_LINE(v_txt);
END;

PLSQL S4L2 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 8
Conditional Control: Case Statements
CASE Expression Syntax
• A CASE expression is different from a CASE statement
because it selects one of a number of results and
assigns it to a variable.
• A CASE expression ends with END not END CASE.
• In the syntax, expressionN can be a literal value or an
expression such as (v_other_var * 2).
variable_name :=
CASE selector
WHEN expression1 THEN result1
WHEN expression2 THEN result2
...
WHEN expressionN THEN resultN
[ELSE resultN+1]
END;

PLSQL S4L2 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 11
Conditional Control: Case Statements
CASE Expression Example
What would be the result of this code if v_grade was
initialized as "C" instead of "A."
DECLARE
RESULT:
v_grade CHAR(1) := 'A';
Grade: A
v_appraisal VARCHAR2(20);
Appraisal: Excellent
BEGIN
v_appraisal :=
Statement processed.
CASE v_grade
WHEN 'A' THEN 'Excellent'
WHEN 'B' THEN 'Very Good'
WHEN 'C' THEN 'Good'
ELSE 'No such grade'
END;
DBMS_OUTPUT.PUT_LINE('Grade: ' || v_grade ||
' Appraisal: ' || v_appraisal);
END;

PLSQL S4L2 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 12
Conditional Control: Case Statements
Logic Tables
• When using IF and CASE statements you often need
to combine conditions using AND, OR, and NOT.
• The following Logic Table displays the results of all
possible combinations of two conditions.
• Example: TRUE and FALSE is FALSE.
AND TRUE FALSE NULL OR TRUE FALSE NULL NOT

Ex.
TRUE TRUE NULL TRUE TRUE TRUE TRUE TRUE FALSE
FALSE

FALSE FALSE FALSE FALSE FALSE TRUE FALSE NULL FALSE TRUE

NULL NULL FALSE NULL NULL TRUE NULL NULL NULL NULL

PLSQL S4L2 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 18
Conditional Control: Case Statements
Boolean Conditions
• What is the value of v_flag in each case?

v_flag := v_reorder_flag AND v_available_flag;

V_REORDER_FLAG V_AVAILABLE_FLAG V_FLAG


TRUE TRUE 1. _____

TRUE FALSE 2. _____

NULL TRUE 3. _____

NULL FALSE 4. _____

PLSQL S4L2 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 19
Conditional Control: Case Statements
Database Programming with
PL/SQL
4-3
Iterative Control: Basic Loops

Copyright © 2018, Oracle and/or its affiliates. All rights reserved.


Iterative Control: LOOP Statements
• Loops repeat a statement or a sequence of statements
multiple times.
• PL/SQL provides the following types of loops:
– Basic loops that perform repetitive actions without overall
conditions
– FOR loops that perform iterative actions based on a counter
– WHILE loops that perform repetitive actions based on a
condition

PLSQL S4L3 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 5
Iterative Control: Basic Loops
Basic Loops
• The simplest form of a LOOP statement is the basic
loop, which encloses a sequence of statements
between the keywords LOOP and END LOOP.
• Use the basic loop when the statements inside the loop
must execute at least once.

PLSQL S4L3 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 6
Iterative Control: Basic Loops
Basic Loops Exit
• Each time the flow of execution reaches the END LOOP
statement, control is passed to the corresponding LOOP
statement that introduced it.
• A basic loop allows the execution of its statements at least
once, even if the EXIT condition is already met upon
entering the loop.
• Without the EXIT statement, the loop would never end (an
infinite loop).
BEGIN
LOOP
statements;
EXIT [WHEN condition];
END LOOP;
END;

PLSQL S4L3 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 7
Iterative Control: Basic Loops
Basic Loops Simple Example
• In this example, no data is processed.
• We simply display the loop counter each time we repeat
the loop.
DECLARE
v_counter NUMBER(2) := 1;
BEGIN
LOOP
DBMS_OUTPUT.PUT_LINE('Loop execution #' || v_counter);
v_counter := v_counter + 1;
EXIT WHEN v_counter > 5;
END LOOP;
END;

PLSQL S4L3 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 8
Iterative Control: Basic Loops
Basic Loops More Complex Example
In this example, three new location IDs for Montreal,
Canada, are inserted in the LOCATIONS table.

DECLARE
v_loc_id locations.location_id%TYPE;
v_counter NUMBER(2) := 1;
BEGIN
SELECT MAX(location_id) INTO v_loc_id FROM locations
WHERE country_id = 2;
LOOP
INSERT INTO locations(location_id, city, country_id)
VALUES((v_loc_id + v_counter), 'Montreal', 2);
v_counter := v_counter + 1;
EXIT WHEN v_counter > 3;
END LOOP;
END;

PLSQL S4L3 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 9
Iterative Control: Basic Loops
Basic Loops EXIT Statement
• You can use the EXIT statement to terminate a loop
and pass control to the next statement after the END
LOOP statement.
• You can issue EXIT as an action within an IF
statement.
DECLARE
v_counter NUMBER := 1;
BEGIN
LOOP
DBMS_OUTPUT.PUT_LINE('Counter is ' || v_counter);
v_counter := v_counter + 1;
IF v_counter > 10 THEN EXIT;
END IF;
END LOOP;
END;

PLSQL S4L3 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 10
Iterative Control: Basic Loops
Basic Loop EXIT Statement Rules
Rules:
• The EXIT statement must be placed inside a loop.
• If the EXIT condition is placed at the top of the loop
(before any of the other executable statements) and
that condition is initially true, then the loop exits and
the other statements in the loop never execute.
• A basic loop can contain multiple EXIT statements.

PLSQL S4L3 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 11
Iterative Control: Basic Loops
Basic Loop EXIT WHEN Statement
• Although the IF…THEN EXIT works to end a loop, the
correct way to end a basic loop is with the EXIT WHEN
statement.
• If the WHEN clause evaluates to TRUE, the loop ends
and control passes to the next statement following END
LOOP.
DECLARE
v_counter NUMBER := 1;
BEGIN
LOOP
DBMS_OUTPUT.PUT_LINE('Counter is ' || v_counter);
v_counter := v_counter + 1;
EXIT WHEN v_counter > 10;
END LOOP;
END;

PLSQL S4L3 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 12
Iterative Control: Basic Loops
Database Programming with
PL/SQL
4-4
Iterative Control: WHILE and FOR Loops

Copyright © 2018, Oracle and/or its affiliates. All rights reserved.


WHILE Loops
• You can use the WHILE loop to repeat a sequence of
statements until the controlling condition is no longer
TRUE.
• The condition is evaluated at the start of each iteration.
• The loop terminates when the condition is FALSE or
NULL.
• If the condition is FALSE or NULL at the initial
execution of the loop, then no iterations are performed.
WHILE condition LOOP
statement1;
statement2;
. . .
END LOOP;
PLSQL S4L4 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 5
Iterative Control: WHILE and FOR Loops
WHILE Loops
• In the syntax:
WHILE condition LOOP
statement1;
statement2;
. . .
END LOOP;

• Condition is a Boolean variable or expression (TRUE,


FALSE, or NULL)
• Statement can be one or more PL/SQL or SQL
statements

PLSQL S4L4 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 6
Iterative Control: WHILE and FOR Loops
WHILE
• In the syntax:
WHILE condition LOOP
statement1;
statement2;
. . .
END LOOP;

• If the variables involved in the conditions do not change


during the body of the loop, then the condition remains
TRUE and the loop does not terminate.
• Note: If the condition yields NULL, then the loop is
bypassed and control passes to the statement that
follows the loop.
PLSQL S4L4 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 7
Iterative Control: WHILE and FOR Loops
WHILE Loops
• In this example, three new location IDs for Montreal,
Canada, are inserted in the LOCATIONS table.
• The counter is explicitly declared in this example.
DECLARE
v_loc_id locations.location_id%TYPE;
v_counter NUMBER := 1;
BEGIN
SELECT MAX(location_id) INTO v_loc_id FROM locations
WHERE country_id = 2;
WHILE v_counter <= 3 LOOP
INSERT INTO locations(location_id, city, country_id)
VALUES((v_loc_id + v_counter), 'Montreal', 2);
v_counter := v_counter + 1;
END LOOP;
END;

PLSQL S4L4 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 8
Iterative Control: WHILE and FOR Loops
FOR Loops Described
• FOR loops have the same general structure as the basic
loop.
• In addition, they have a control statement before the
LOOP keyword to set the number of iterations that
PL/SQL performs.

FOR counter IN [REVERSE]


lower_bound..upper_bound LOOP
statement1;
statement2;
. . .
END LOOP;

PLSQL S4L4 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 11
Iterative Control: WHILE and FOR Loops
FOR Loop Rules
FOR loop rules:
• Use a FOR loop to shortcut the test for the number of
iterations.
• Do not declare the counter; it is declared implicitly.
• lower_bound .. upper_bound is the required
syntax.
FOR counter IN [REVERSE]
lower_bound..upper_bound LOOP
statement1;
statement2;
. . .
END LOOP;

PLSQL S4L4 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 12
Iterative Control: WHILE and FOR Loops
FOR Loops Syntax
• Counter is an implicitly declared integer whose value
automatically increases or decreases (decreases if the
REVERSE keyword is used) by 1 on each iteration of
the loop until the upper or lower bound is reached.
• REVERSE causes the counter to decrement with each
iteration from the upper bound to the lower bound.
• (Note that the lower bound is referenced first.)
FOR counter IN [REVERSE]
lower_bound..upper_bound LOOP
statement1;
statement2;
. . .
END LOOP;

PLSQL S4L4 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 13
Iterative Control: WHILE and FOR Loops
FOR Loops Syntax
• lower_bound specifies the lower bound for the range
of counter values.
• upper_bound specifies the upper bound for the
range of counter values.

FOR counter IN [REVERSE]


lower_bound..upper_bound LOOP
statement1;
statement2;
. . .
END LOOP;

PLSQL S4L4 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 14
Iterative Control: WHILE and FOR Loops
FOR Loop Guidelines
FOR loops are a common structure of programming
languages.
• A FOR loop is used within the code when the beginning
and ending value of the loop is known.
• Reference the counter only within the loop; its scope
does not extend outside the loop.
• Do not reference the counter as the target of an
assignment.
• Neither loop bound (lower or upper) should be NULL.

PLSQL S4L4 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 16
Iterative Control: WHILE and FOR Loops
Guidelines For When to Use Loops
• 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.

PLSQL S4L4 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 18
Iterative Control: WHILE and FOR Loops
Database Programming with
PL/SQL
4-5
Iterative Control: Nested Loops

Copyright © 2018, Oracle and/or its affiliates. All rights reserved.


Nested Loop Example
• In PL/SQL, you can nest loops to multiple levels.
• You can nest FOR, WHILE, and basic loops within one
another.
BEGIN
FOR v_outerloop IN 1..3 LOOP
FOR v_innerloop IN REVERSE 1..5 LOOP
DBMS_OUTPUT.PUT_LINE('Outer loop is: ' ||
v_outerloop ||
' and inner loop is: ' ||
v_innerloop);
END LOOP;
END LOOP;
END;

PLSQL S4L5 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 5
Iterative Control: Nested Loops
Nested Loops
• This example contains EXIT conditions in nested basic
loops.
• What if you want to exit from the outer loop at step A?
DECLARE
v_outer_done CHAR(3) := 'NO';
v_inner_done CHAR(3) := 'NO';
BEGIN
LOOP -- outer loop
...
LOOP -- inner loop
...
... -- step A
EXIT WHEN v_inner_done = 'YES';
...
END LOOP;
...
EXIT WHEN v_outer_done = 'YES';
...
END LOOP;
END;

PLSQL S4L5 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 6
Iterative Control: Nested Loops
Loop Labels
Loop labels are required in this example in order to exit
an outer loop from within an inner loop
DECLARE
...
BEGIN
<<outer_loop>>
LOOP -- outer loop
...
<<inner_loop>>
LOOP -- inner loop
EXIT outer_loop WHEN ... -- exits both loops
EXIT WHEN v_inner_done = 'YES';
...
END LOOP;
...
EXIT WHEN v_outer_done = 'YES'; ...
END LOOP;
END;

PLSQL S4L5 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 7
Iterative Control: Nested Loops
Loop Labels
• Loop label names follow the same rules as other
identifiers.
• A label is placed before a statement, either on the same
line or on a separate line.
• In FOR or WHILE loops, place the label before FOR or
WHILE within label delimiters (<<label>>).
• If the loop is labeled, the label name can optionally be
included after the END LOOP statement for clarity.

PLSQL S4L5 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 8
Iterative Control: Nested Loops
Loop Labels
Label basic loops by placing the label before the word
LOOP within label delimiters (<<label>>).
DECLARE
v_outerloop PLS_INTEGER := 0;
v_innerloop PLS_INTEGER := 5;
BEGIN
<<outer_loop>>
LOOP
v_outerloop := v_outerloop + 1;
v_innerloop := 5;
EXIT WHEN v_outerloop > 3;
<<inner_loop>>
LOOP
DBMS_OUTPUT.PUT_LINE('Outer loop is: ' || v_outerloop ||
' and inner loop is: ' || v_innerloop);
v_innerloop := v_innerloop - 1;
EXIT WHEN v_innerloop = 0;
END LOOP inner_loop;
END LOOP outer_loop;
END;

PLSQL S4L5 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 9
Iterative Control: Nested Loops
Nested Loops and Labels
• In this example, there are two loops.
• The outer loop is identified by the label
<<outer_loop>>, and the inner loop is identified by
the label <<inner_loop>>.
• We reference the outer loop in the EXIT statement
from within the ...BEGIN
<<outer_loop>>
inner_loop. LOOP
v_counter := v_counter + 1;
EXIT WHEN v_counter > 10;
<<inner_loop>>
LOOP ...
EXIT outer_loop WHEN v_total_done = 'YES';
-- Leave both loops
EXIT WHEN v_inner_done = 'YES';
-- Leave inner loop only ...
END LOOP inner_loop; ...
END LOOP outer_loop;
END;

PLSQL S4L5 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 10
Iterative Control: Nested Loops

Das könnte Ihnen auch gefallen