Tải bản đầy đủ (.pdf) (34 trang)

Tài liệu Controlling flow in PL / SQL pptx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (197.54 KB, 34 trang )

Controlling Flow in PL/SQL
Blocks
23
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder23Ć2
Controlling Flow in PL/SQL Blocks 23Ć3
Objectives
You can control the flow of your PL/SQL block by using conditional statements
and loops.
At the end of this lesson, you should be able to
D Conditionally control processing in a PL/SQL block.
D Iterate statements by using various types of loops.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder23Ć4
Controlling Flow in PL/SQL Blocks 23Ć5
Overview
You can change the logical flow of statements within the PL/SQL block with a
number of control structures. This lesson addresses two types of PL/SQL control
structures:
D Conditional constructs with the IF statement
D Looping constructs
D Basic loop to provide repetitive actions without overall conditions
D FOR loops to provide for iterative control of actions based upon a count
D WHILE loops to provide iterative control of actions based on a true statement
D EXIT statement to terminate loops
For more information, see
PL/SQL User’s Guide and Reference, Release 2.3, Chapter 3 “Control Structures.”
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder23Ć6
Controlling Flow in PL/SQL Blocks 23Ć7
The IF Statement
The structure of the PL/SQL IF statement is similar to the structure of IF statements
in other procedural languages. It allows PL/SQL to perform actions selectively based
upon conditions.


Syntax
IF condition THEN
statements;
[ELSIF condition THEN
statements;]
[ELSE
statements;]
END IF;
where: condition is a Boolean variable or expression (TRUE,
FALSE, or NULL).
Guidelines
D When writing code, remember the spelling of the keywords.
D ELSIF is one word.
D END IF is two words.
D If the controlling Boolean condition is TRUE, the associated sequence of
statements is executed; if the controlling Boolean condition is FALSE or NULL,
the associated sequence of statements is passed over.
D Any number of ELSIF clauses are permitted.
D There can be at most one ELSE clause.
D Indent the conditionally executed statements for clarity.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder23Ć8
Controlling Flow in PL/SQL Blocks 23Ć9
The IF Statement continued
Simple IF Statements
PL/SQL executes the conditional statements only if the condition is TRUE. If the
condition is FALSE or NULL, then PL/SQL ignores the conditional statements. In
either case, control resumes at the next statement in the program following END IF.
Example
Set the job title to Sales Representative and the region number to 35 if the last name
is Dumas.

. . .
IF v_last_name = ’Dumas’ THEN
v_job := ’Sales Representative’;
v_region_id := 35;
END IF;
. . .
IFĆTHENĆELSE Statements
If the condition is FALSE or NULL, you can use the ELSE clause to carry out other
actions. As with the simple IF statement, control resumes in the program from the
END IF.
Example
Set a flag for orders where there are fewer than five days between order date and ship
date.
. . .
IF v_date_shipped - v_date_ordered < 5 THEN
v_ship_flag := ’Acceptable’;
ELSE
v_ship_flag := ’Unacceptable’;
END IF;
. . .
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder23Ć10
Controlling Flow in PL/SQL Blocks 23Ć11
The IF Statement continued
Nested IF Statements
Either set of actions of the result of the first IF statement can include further IF
statements before specific actions are performed. Each nested IF statement must be
terminated with a corresponding END IF.
IFĆTHENĆELSIF Statements
When possible, however, use the ELSIF clause instead of nesting IF statements. The
code is easier to read and understand. The logic is clearly identified. If the action in

the ELSE clause consists purely of another IF statement, it is more convenient to use
the ELSIF clause. This makes the code clearer by removing the need for nested END
IFs at the end of each further set of conditions and actions.
Example
For a given value entered, return a calculated value. If the entered value is over 100,
then the calculated value is two times the entered value. If the entered value is
between 50 and 100, then the calculated value is 50% of the starting value. If the
entered value is less than 50, then the calculated value is 10% of the starting value.
FUNCTION calc_val
(v_start IN NUMBER)
RETURN NUMBER
IS
BEGIN
IF v_start > 100 THEN
RETURN (2 * v_start);
ELSIF v_start >= 50 THEN
RETURN (.5 * v_start);
ELSE
RETURN (.1 * v_start);
END IF;
END calc_val;
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder23Ć12
Controlling Flow in PL/SQL Blocks 23Ć13
Building Logical Conditions
Build a simple Boolean condition by combining number, character, or date
expressions with a comparison operator. In general, handle null values with the IS
NULL operator.
Null Within Expressions and Comparisons
D Any expression containing a null value evaluates to NULL, with the exception of
a concatenated expression, which treats the null value as the empty string.

D Any simple comparison containing a null value evaluates to NULL.
D An IS NULL comparison evaluates to TRUE or FALSE.
Boolean Conditions with Logical Operators
Build a complex Boolean condition by combining simple Boolean conditions with the
logical operators AND, OR, and NOT. In the accompanying logic tables, FALSE
takes precedence for an AND condition and TRUE takes precedence in an OR
condition.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder23Ć14
Controlling Flow in PL/SQL Blocks 23Ć15
Building Logical Conditions continued
The AND logic table can help you evaluate the possibilities for the Boolean condition
you see below.
. . .
v_flag := v_reorder_flag AND v_available_flag;
. . .
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder23Ć16
Controlling Flow in PL/SQL Blocks 23Ć17
Loop Statements
PL/SQL provides a number of facilities to structure loops to repeat a statement or
sequence of statements multiple times.
Basic Loop
The simplest loop consists of the body of statements to be repeated enclosed between
the delimiters LOOP and END LOOP. Each time the flow of execution reaches the
END LOOP statement, control is returned to the corresponding LOOP statement
above it. This uncontrolled loop is an infinite loop that is to be avoided. To avoid an
infinite loop, add an EXIT statement.
The EXIT Statement
You can terminate a loop using the EXIT statement. Control passes to the next
statement after the END LOOP statement. You can issue EXIT either as an action
within an IF statement, or as a standalone statement within the loop. In the latter case,

you can attach a WHEN clause to allow conditional termination of the loop.
Syntax
LOOP
statement1;
statement2;
. . .
EXIT [WHEN condition];
END LOOP;
where: condition is a Boolean variable or expression (TRUE,
FALSE, or NULL).
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder23Ć18
Controlling Flow in PL/SQL Blocks 23Ć19
Loop Statements continued
Example
Insert the first ten new line items for order number 101.
. . .
v_ord_id s_item.ord_id%TYPE := 101;
v_counter NUMBER (2) := 1;
BEGIN
. . .
LOOP
INSERT INTO s_item (ord_id, item_id)
VALUES (v_ord_id, v_counter);
v_counter := v_counter + 1;
EXIT WHEN v_counter > 10;
END LOOP;
. . .
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder23Ć20
Controlling Flow in PL/SQL Blocks 23Ć21
Loop Statements continued

FOR Loop
FOR loops have the same general structure as the loops you have already seen. In
addition, they have a control statement at the front of the LOOP keyword to
determine the number of iterations PL/SQL performs.
Syntax
FOR index IN [REVERSE] lower_bound upper_bound LOOP
statement1;
statement2;
. . .
END LOOP;
where: index is an implicitly declared integer whose value
automatically increases or decreases by 1 on
each iteration of the loop until the upper bound
is reached.
REVERSE causes the index to decrement with each
iteration from the upper bound to the lower
bound.
lower_bound specifies the lower bound for the range of index
values.
upper_bound specifies the upper bound for the range of index
values.
Note: Do not declare the index; it is declared implicitly as an integer.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder23Ć22
Controlling Flow in PL/SQL Blocks 23Ć23
Loop Statements continued
Example
Print the number of times the loop is executed and the last value for the index based
on the supplied lower bound and upper bound.
PROCEDURE iterate
(v_lower NUMBER,

v_upper NUMBER)
IS
v_counter NUMBER(10) := 0;
v_output NUMBER(10);
BEGIN
FOR i IN v_lower v_upper LOOP
v_counter := v_counter + 1;
v_output := i;
END LOOP;
TEXT_IO.PUT_LINE(’Last value is ’||TO_CHAR(v_output)
||’. Total loops = ’||TO_CHAR(v_counter));
END iterate;
Guidelines
D Reference the index within the loop only; it is undefined outside the loop.
D Reference the existing value of an index within an expression.
D Do not reference the index as the target of an assignment.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder23Ć24
Condition is
evaluated at the
beginning of
each iteration.
Controlling Flow in PL/SQL Blocks 23Ć25
Loop Statements continued
WHILE Loop
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. If the condition is FALSE at the
start of the loop, then no further iterations are performed.
Syntax
WHILE condition LOOP

statement1;
statement2;
. . .
END LOOP;
where: condition is a Boolean variable or expression (TRUE,
FALSE, or NULL).
If the variables involved in the conditions do not change during the body of the loop,
then the condition will remain TRUE, and the loop will not terminate.

×