Sie sind auf Seite 1von 14

PBD Oracle Structuri de control-1

Copyright Oracle Corporation, 1998. All rights reserved.


19
19
Writing Control Structures
Writing Control Structures
19-2 Copyright Oracle Corporation, 1998. All rights reserved.
Objectives
Objectives
After completing this lesson, you should
be able to do the following:
Identify the uses and types of control
structures
Construct an IF statement
Construct and identify different loop
statements
Use logic tables
Control block flow using nested loops
and labels
After completing this lesson, you should After completing this lesson, you should
be able to do the following: be able to do the following:
Identify the uses and types of control
structures
Construct an IF statement
Construct and identify different loop
statements
Use logic tables
Control block flow using nested loops
and labels
PBD Oracle Structuri de control-2
19-3 Copyright Oracle Corporation, 1998. All rights reserved.
Controlling PL/SQL Flow of
Execution
Controlling PL/SQL Flow of
Execution
You can change the logical flow of
statements using conditional IF
statements and loop control structures.
Conditional IF statements:
IF-THEN-END IF
IF-THEN-ELSE-END IF
IF-THEN-ELSIF-END IF
You can change the logical flow of You can change the logical flow of
statements using conditional IF statements using conditional IF
statements and loop control structures. statements and loop control structures.
Conditional IF Conditional IF statements: statements:
IF-THEN-END IF
IF-THEN-ELSE-END IF
IF-THEN-ELSIF-END IF
19-4 Copyright Oracle Corporation, 1998. All rights reserved.
IF Statements
IF Statements
IF condition THEN
statements;
[ELSIF condition THEN
statements;]
[ELSE
statements;]
END IF;
IF condition THEN
statements;
[ELSIF condition THEN
statements;]
[ELSE
statements;]
END IF;
Syntax
Simple IF Statement:
Set the manager ID to 22 if the employee
name is Osborne.
Syntax Syntax
Simple IF Statement: Simple IF Statement:
Set the manager ID to 22 if the employee Set the manager ID to 22 if the employee
name is Osborne. name is Osborne.
IF v_ename = 'OSBORNE' THEN
v_mgr := 22;
END IF;
IF v_ename = 'OSBORNE' THEN
v_mgr := 22;
END IF;
PBD Oracle Structuri de control-3
19-5 Copyright Oracle Corporation, 1998. All rights reserved.
Simple IF Statements
Simple IF Statements
Set the job title to Salesman, the
department number to 35, and the
commission to 20%of the current salary if
the last name is Miller.
Example
Set the job title to Salesman, the Set the job title to Salesman, the
department number to 35, and the department number to 35, and the
commission to 20%of the current salary if commission to 20%of the current salary if
the last name is Miller. the last name is Miller.
Example Example
. . .
IF v_ename = 'MILLER' THEN
v_job := 'SALESMAN';
v_deptno := 35;
v_new_comm := sal * 0.20;
END IF;
. . .
. . .
IF v_ename = 'MILLER' THEN
v_job := 'SALESMAN';
v_deptno := 35;
v_new_comm := sal * 0.20;
END IF;
. . .
19-6 Copyright Oracle Corporation, 1998. All rights reserved.
IF-THEN-ELSE Statement
Execution Flow
IF-THEN-ELSE Statement
Execution Flow
IF Condition IF Condition
TRUE TRUE
THEN Actions
(including further IFs)
THEN Actions THEN Actions
(including further (including further IFs IFs) )
FALSE FALSE
ELSE actions
(including further IFs)
ELSE actions ELSE actions
(including further (including further IFs IFs) )
PBD Oracle Structuri de control-4
19-7 Copyright Oracle Corporation, 1998. All rights reserved.
IF-THEN-ELSE Statements
IF-THEN-ELSE Statements
Set a flag for orders where there are fewer
than 5 days between order date and ship
date.
Example
Set a flag for orders where there are fewer Set a flag for orders where there are fewer
than 5 days between order date and ship than 5 days between order date and ship
date. date.
Example Example
...
IF v_shipdate - v_orderdate < 5 THEN
v_ship_flag := 'Acceptable';
ELSE
v_ship_flag := 'Unacceptable';
END IF;
...
...
IF v_shipdate - v_orderdate < 5 THEN
v_ship_flag := 'Acceptable';
ELSE
v_ship_flag := 'Unacceptable';
END IF;
...
19-8 Copyright Oracle Corporation, 1998. All rights reserved.
IF-THEN-ELSIF
Statement Execution Flow
IF-THEN-ELSIF
Statement Execution Flow
IF Condition
IF Condition IF Condition
TRUE TRUE
THEN Actions
THEN Actions THEN Actions
FALSE FALSE
ELSIF
Condition
ELSIF ELSIF
Condition Condition
TRUE TRUE
THEN Actions
THEN Actions THEN Actions
FALSE FALSE
ELSE
Actions
ELSE ELSE
Actions Actions
PBD Oracle Structuri de control-5
19-9 Copyright Oracle Corporation, 1998. All rights reserved.
IF-THEN-ELSIF Statements
IF-THEN-ELSIF Statements
For a given value entered, return a
calculated value.
Example
For a given value entered, return a For a given value entered, return a
calculated value. calculated value.
Example Example
. . .
IF v_start > 100 THEN
v_start := 2 * v_start;
ELSIF v_start >= 50 THEN
v_start := .5 * v_start;
ELSE
v_start := .1 * v_start;
END IF;
. . .
. . .
IF v_start > 100 THEN
v_start := 2 * v_start;
ELSIF v_start >= 50 THEN
v_start := .5 * v_start;
ELSE
v_start := .1 * v_start;
END IF;
. . .
19-10 Copyright Oracle Corporation, 1998. All rights reserved.
Building Logical Conditions
Building Logical Conditions
You can handle null values with the IS
NULL operator.
Any expression containing a null value
evaluates to NULL.
Concatenated expressions with null
values treat null values as an empty
string.
You can handle null values with the IS
NULL operator.
Any expression containing a null value
evaluates to NULL.
Concatenated expressions with null
values treat null values as an empty
string.
PBD Oracle Structuri de control-6
19-11 Copyright Oracle Corporation, 1998. All rights reserved.
Logic Tables
Logic Tables
Build a simple Boolean condition with a
comparison operator.
Build a simple Boolean condition with a Build a simple Boolean condition with a
comparison operator. comparison operator.
NOT
TRUE
FALSE
NULL
OR
TRUE
FALSE
NULL
TRUE FALSE NULL
FALSE
TRUE
NULL
AND
TRUE
FALSE
NULL
TRUE FALSE NULL
TRUE
NULL NULL
NULL
FALSE FALSE
FALSE
FALSE
FALSE
TRUE
TRUE
TRUE
TRUE TRUE
FALSE
NULL NULL
NULL
19-12 Copyright Oracle Corporation, 1998. All rights reserved.
Boolean Conditions
Boolean Conditions
What is the value of V_FLAG in each case?
What is the value of V_FLAG in each case? What is the value of V_FLAG in each case?
V_REORDER_FLAG V_AVAILABLE_FLAG V_FLAG
TRUE TRUE
TRUE FALSE
NULL TRUE
NULL FALSE
v_flag := v_reorder_flag AND v_available_flag;
v_flag := v_reorder_flag AND v_available_flag;
TRUE TRUE
FALSE FALSE
NULL NULL
FALSE FALSE
PBD Oracle Structuri de control-7
19-13 Copyright Oracle Corporation, 1998. All rights reserved.
Iterative Control: LOOP
Statements
Iterative Control: LOOP
Statements
Loops repeat a statement or sequence
of statements multiple times.
There are three loop types:
Basic loop
FOR loop
WHILE loop
Loops repeat a statement or sequence
of statements multiple times.
There are three loop types:
Basic loop
FOR loop
WHILE loop
19-14 Copyright Oracle Corporation, 1998. All rights reserved.
Basic Loop
Basic Loop
Syntax
Syntax Syntax
LOOP
statement1;
. . .
EXIT [WHEN condition];
END LOOP;
LOOP
statement1;
. . .
EXIT [WHEN condition];
END LOOP;
where: condition is a Boolean variable or
expression (TRUE, FALSE,
or NULL);
where: condition is a Boolean variable or
expression (TRUE, FALSE,
or NULL);
-- -- delimiter delimiter
-- -- statements statements
-- -- EXIT statement EXIT statement
-- -- delimiter delimiter
PBD Oracle Structuri de control-8
19-15 Copyright Oracle Corporation, 1998. All rights reserved.
Basic Loop
Basic Loop
DECLARE
v_ordid item.ordid%TYPE := 101;
v_counter NUMBER(2) := 1;
BEGIN
LOOP
INSERT INTO item(ordid, itemid)
VALUES(v_ordid, v_counter);
v_counter := v_counter + 1;
EXIT WHEN v_counter > 10;
END LOOP;
END;
DECLARE
v_ordid item.ordid%TYPE := 101;
v_counter NUMBER(2) := 1;
BEGIN
LOOP
INSERT INTO item(ordid, itemid)
VALUES(v_ordid, v_counter);
v_counter := v_counter + 1;
EXIT WHEN v_counter > 10;
END LOOP;
END;
Example
Example Example
19-16 Copyright Oracle Corporation, 1998. All rights reserved.
FOR Loop
FOR Loop
Syntax
Use a FOR loop to shortcut the test for
the number of iterations.
Do not declare the index; it is declared
implicitly.
Syntax Syntax
Use a FOR loop to shortcut the test for
the number of iterations.
Do not declare the index; it is declared
implicitly.
FOR counter in [REVERSE]
lower_bound..upper_bound LOOP
statement1;
statement2;
. . .
END LOOP;
FOR counter in [REVERSE]
lower_bound..upper_bound LOOP
statement1;
statement2;
. . .
END LOOP;
PBD Oracle Structuri de control-9
19-17 Copyright Oracle Corporation, 1998. All rights reserved.
FOR Loop
FOR Loop
Guidelines
Reference the counter within the loop
only; it is undefined outside the loop.
Use an expression to reference the
existing value of a counter.
Do not reference the counter as the target
of an assignment.
Guidelines Guidelines
Reference the counter within the loop
only; it is undefined outside the loop.
Use an expression to reference the
existing value of a counter.
Do not reference the counter as the target
of an assignment.
19-18 Copyright Oracle Corporation, 1998. All rights reserved.
FOR Loop
FOR Loop
Insert the first 10 new line items for order
number 101.
Example
Insert the first 10 new line items for order Insert the first 10 new line items for order
number 101. number 101.
Example Example
DECLARE
v_ordid item.ordid%TYPE := 101;
BEGIN
FOR i IN 1..10 LOOP
INSERT INTO item(ordid, itemid)
VALUES(v_ordid, i);
END LOOP;
END;
DECLARE
v_ordid item.ordid%TYPE := 101;
BEGIN
FOR i IN 1..10 LOOP
INSERT INTO item(ordid, itemid)
VALUES(v_ordid, i);
END LOOP;
END;
PBD Oracle Structuri de control-10
19-19 Copyright Oracle Corporation, 1998. All rights reserved.
WHILE Loop
WHILE Loop
Syntax
Use the WHILE loop to repeat statements
while a condition is TRUE.
Syntax Syntax
Use the WHILE loop to repeat statements Use the WHILE loop to repeat statements
while a condition is TRUE. while a condition is TRUE.
WHILE condition LOOP
statement1;
statement2;
. . .
END LOOP;
WHILE condition LOOP
statement1;
statement2;
. . .
END LOOP;
Condition is Condition is
evaluated at the evaluated at the
beginning of beginning of
each iteration. each iteration.
19-20 Copyright Oracle Corporation, 1998. All rights reserved.
WHILE Loop
WHILE Loop
Example
Example Example
ACCEPT p_price PROMPT 'Enter the price of the item: '
ACCEPT p_itemtot PROMPT 'Enter the maximum total for
purchase of item: '
DECLARE
...
v_qty NUMBER(8) := 1;
v_running_total NUMBER(7,2) := 0;
BEGIN
...
WHILE v_running_total < &p_itemtot LOOP
...
v_qty := v_qty + 1;
v_running_total := v_qty * &p_price;
END LOOP;
...
ACCEPT p_price PROMPT 'Enter the price of the item: '
ACCEPT p_itemtot PROMPT 'Enter the maximum total for
purchase of item: '
DECLARE
...
v_qty NUMBER(8) := 1;
v_running_total NUMBER(7,2) := 0;
BEGIN
...
WHILE v_running_total < &p_itemtot LOOP
...
v_qty := v_qty + 1;
v_running_total := v_qty * &p_price;
END LOOP;
...
PBD Oracle Structuri de control-11
19-21 Copyright Oracle Corporation, 1998. All rights reserved.
Nested Loops and Labels
Nested Loops and Labels
Nest loops to multiple levels.
Use labels to distinguish between
blocks and loops.
Exit the outer loop with the EXIT
statement referencing the label.
Nest loops to multiple levels.
Use labels to distinguish between
blocks and loops.
Exit the outer loop with the EXIT
statement referencing the label.
19-22 Copyright Oracle Corporation, 1998. All rights reserved.
Nested Loops and Labels
Nested Loops and Labels
...
BEGIN
<<Outer_loop>>
LOOP
v_counter := v_counter+1;
EXIT WHEN v_counter>10;
<<Inner_loop>>
LOOP
...
EXIT Outer_loop WHEN total_done = 'YES';
-- Leave both loops
EXIT WHEN inner_done = 'YES';
-- Leave inner loop only
...
END LOOP Inner_loop;
...
END LOOP Outer_loop;
END;
...
BEGIN
<<Outer_loop>>
LOOP
v_counter := v_counter+1;
EXIT WHEN v_counter>10;
<<Inner_loop>>
LOOP
...
EXIT Outer_loop WHEN total_done = 'YES';
-- Leave both loops
EXIT WHEN inner_done = 'YES';
-- Leave inner loop only
...
END LOOP Inner_loop;
...
END LOOP Outer_loop;
END;
PBD Oracle Structuri de control-12
19-23 Copyright Oracle Corporation, 1998. All rights reserved.
Change the logical flow of statements by
using control structures.
Conditional (IF statement)
Loops
Basic loop
FOR loop
WHILE loop
EXIT statement
Change the logical flow of statements by Change the logical flow of statements by
using control structures. using control structures.
Conditional (IF statement)
Loops
Basic loop
FOR loop
WHILE loop
EXIT statement
Summary
Summary
19-24 Copyright Oracle Corporation, 1998. All rights reserved.
Practice Overview
Practice Overview
Performing conditional actions using
the IF statement
Performing iterative steps using the
loop structure
Performing conditional actions using
the IF statement
Performing iterative steps using the
loop structure
PBD Oracle Structuri de control-13
19-25 Copyright Oracle Corporation, 1998. All rights reserved.
19-26 Copyright Oracle Corporation, 1998. All rights reserved.
PBD Oracle Structuri de control-14
19-27 Copyright Oracle Corporation, 1998. All rights reserved.
19-28 Copyright Oracle Corporation, 1998. All rights reserved.

Das könnte Ihnen auch gefallen