0% found this document useful (0 votes)
27 views38 pages

Lesson 6

hi i need this document for education porpose
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views38 pages

Lesson 6

hi i need this document for education porpose
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 38

Lesson 6

Controlling Flow (Selection and


Iteration) in PL/SQL Blocks
Topic Coved

• DBMS and RDBMS


• Installation of Oracle Database 21c XE and Oracle SQL Developer
• Oracle SQL (Querying data, Sorting data, Filtering data)
• Grouping and Joining tables and Sub queries
• Views, Indexes and Sequences
• Regular Expressions
• PL/SQL Blokes
• Control Structure, Iterative processing with loops, Select Into in PL/SQL
• Exception handlers
• Cursors, Records and Stored procedures and Functions
• Program Units
• Database Triggers
• PL/SQL Collections
• Dynamic SQL

2-18

.Oracle Corporation, 1996. All rights reserved ‫ س‬Copyright


Objectives

• Conditionally control processing in a PL/SQL


block.
• Iterate statements by using various types of loops.

3-23

.Oracle Corporation, 1996. All rights reserved ‫ س‬Copyright


Controlling PL/SQL Flow of Execution

Change the logical flow of statements by using


control structures:
There are two types of Control structure statements

• Selection statements
Conditional control structures (IF statement)]

• Iteration statements
Loop control structures
– Basic loop
– FOR loop
4-23
– WHILE loop
– EXIT AND CONTINUE statement
.Oracle Corporation, 1996. All rights reserved ‫ س‬Copyright
The IF Statement: Syntax
• You can perform actions selectively based upon
conditions being met.
IF
IF condition
condition THEN
THEN
statements
statements;;
[ELSIF
[ELSIF condition
condition THEN
THEN
statements
statements;]
;]
[ELSE
[ELSE
statements
statements;]
;]
END
END IF;
IF;

• ELSIF is one word.


• END IF is two words.
5-23
• At most, one ELSE clause is permitted.
.Oracle Corporation, 1996. All rights reserved ‫ س‬Copyright
Simple IF Statements: Example

Set the job title to Sales Representative and the


region number to 35 if the last name is Dumas.
.. .. ..
IF
IF v_last_name
v_last_name == 'Dumas'
'Dumas' THEN
THEN
v_job
v_job :=
:= 'Sales
'Sales Representative';
Representative';
v_region_id
v_region_id :=
:= 35;
35;
END
END IF;
IF;
.. .. ..

6-23

.Oracle Corporation, 1996. All rights reserved ‫ س‬Copyright


IF-THEN-ELSE Statements: Example

Set a flag for orders where there are fewer than 5


days between order date and ship date.
.. .. ..
IF
IF v_date_shipped
v_date_shipped -- v_date_ordered
v_date_ordered << 55 THEN
THEN
v_ship_flag
v_ship_flag :=
:= 'Acceptable';
'Acceptable';
ELSE
ELSE
v_ship_flag
v_ship_flag :=
:= 'Unacceptable';
'Unacceptable';
END
END IF;
IF;
.. .. ..

7-23

.Oracle Corporation, 1996. All rights reserved ‫ س‬Copyright


IF-THEN-ELSIF Statements: Example

For a given value entered, return a calculated value.


.. .. ..
IF
IF v_start
v_start >> 100
100 THEN
THEN
RETURN
RETURN (2
(2 ** v_start);
v_start);
ELSIF
ELSIF v_start
v_start >=
>= 50
50 THEN
THEN
RETURN
RETURN (.5
(.5 ** v_start);
v_start);
ELSE
ELSE
RETURN
RETURN (.1
(.1 ** v_start);
v_start);
END
END IF;
IF;
.. .. ..

8-23

.Oracle Corporation, 1996. All rights reserved ‫ س‬Copyright


PL/SQL IF THEN statement

The following illustrates the structure of the IF THEN


:statement

IF condition THEN
;statements
;END IF

9-23

.Oracle Corporation, 1996. All rights reserved ‫ س‬Copyright


DECLARE
;n_sales NUMBER := 2000000
BEGIN
IF n_sales > 100000 THEN
DBMS_OUTPUT.PUT_LINE
;) ' Sales revenue is greater than 100K' (
;END IF
;END

10-23

.Oracle Corporation, 1996. All rights reserved ‫ س‬Copyright


PL/SQL IF THEN ELSE statement
IF condition THEN
;statements
ELSE
;else_statements
;END IF

11-23

.Oracle Corporation, 1996. All rights reserved ‫ س‬Copyright


If and else example

DECLARE
;n_sales NUMBER := 300000
;n_commission NUMBER( 10, 2 ) := 0
BEGIN
IF n_sales > 200000 THEN
;n_commission := n_sales * 0.1
ELSE
;n_commission := n_sales * 0.05
;END IF
;END

12-23

.Oracle Corporation, 1996. All rights reserved ‫ س‬Copyright


PL/SQL IF THEN ELSIF statement

IF condition_1 THEN
statements_1
ELSIF condition_2 THEN
statements_2
... ] ELSIF condition_3 THEN statements_3 [
] ELSE else_statements [
;END IF

13-23

.Oracle Corporation, 1996. All rights reserved ‫ س‬Copyright


DECLARE
;n_sales NUMBER := 300000
;n_commission NUMBER( 10, 2 ) := 0
BEGIN
IF n_sales > 200000 THEN
;n_commission := n_sales * 0.1
ELSIF n_sales <= 200000 AND n_sales > 100000 THEN
;n_commission := n_sales * 0.05
ELSIF n_sales <= 100000 AND n_sales > 50000 THEN
;n_commission := n_sales * 0.03
;ELSE n_commission := n_sales * 0.02
;END IF
;END
14-23

.Oracle Corporation, 1996. All rights reserved ‫ س‬Copyright


Nested IF statement

IF condition_1 THEN
IF condition_2 THEN
;nested_if_statements
;END IF
ELSE
;else_statements
;END IF

15-23

.Oracle Corporation, 1996. All rights reserved ‫ س‬Copyright


Simple CASE statement

CASE selector
WHEN selector_value_1 THEN
statements_1
WHEN selector_value_1 THEN
... statement_2
ELSE
else_statements
;END CASE

16-23

.Oracle Corporation, 1996. All rights reserved ‫ س‬Copyright


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.

17-23

.Oracle Corporation, 1996. All rights reserved ‫ س‬Copyright


Logic Tables

Build a simple Boolean condition with a comparison


operator.

AND TRUE FALSE NULL OR TRUE FALSE NULL NOT

TRUE TRUE FALSE NULL TRUE TRUE TRUE TRUE TRUE FALSE

FALSE FALSE FALSE FALSE FALSE TRUE FALSE NULL FALSE TRUE

NULL NULL FALSE NULL NULL TRUE NULL NULL NULL NULL

18-23

.Oracle Corporation, 1996. All rights reserved ‫ س‬Copyright


Boolean Conditions

What is the value of V_FLAG in each case?


v_flag
v_flag :=
:= v_reorder_flag
v_reorder_flag AND
AND v_available_flag;
v_available_flag;

V_REORDER_FLAG V_AVAILABLE_FLAG VALUE


TRUE
TRUE
TRUE
FALSE
NULL
TRUE
NULL
FALSE
19-23

.Oracle Corporation, 1996. All rights reserved ‫ س‬Copyright


Consider the following example
:BOOLEAN
DECLARE
;b_profitable BOOLEAN
;n_sales NUMBER
;n_costs NUMBER
BEGIN
;b_profitable := false
IF n_sales > n_costs THEN
;b_profitable := true
;END IF
;END
20-23

.Oracle Corporation, 1996. All rights reserved ‫ س‬Copyright


LOOP Statements

• Loops repeat a statement or sequence of


statements multiple times.
• Three loop types:
– Basic loop
– FOR loop
– WHILE loop
– EXIT AND CONTINUE

21-23

.Oracle Corporation, 1996. All rights reserved ‫ س‬Copyright


Basic Loop: Syntax

• Iterate through your statements with a basic


loop.
LOOP
LOOP -- delimiter
statement1
statement1;; -- statements
.. .. ..
EXIT
EXIT [WHEN
[WHEN condition
condition];
]; -- EXIT statement
END
END LOOP;
LOOP; -- delimiter

• Without the EXIT statement, the loop would be


infinite.

22-23

.Oracle Corporation, 1996. All rights reserved ‫ س‬Copyright


Example of Basic loop
DECLARE
;i number(4):=1
begin
loop
;DBMS_OUTPUT.PUT_LINE ('Inside loop: i = ' || TO_CHAR(i))
;i := i + 1
IF (i > 10) THEN
;EXIT
;END IF
;END LOOP
;end
23-23

.Oracle Corporation, 1996. All rights reserved ‫ س‬Copyright


Basic Loop: Example
Insert the first ten new line items for order number 101.
.. .. ..
v_ord_id
v_ord_id s_item.ord_id%TYPE
s_item.ord_id%TYPE := := 101;
101;
v_counter
v_counter NUMBER(2)
NUMBER(2) :=
:= 1;
1;
BEGIN
BEGIN
.. .. ..
LOOP
LOOP
INSERT
INSERT INTO
INTO s_item
s_item (ord_id,
(ord_id, item_id)
item_id)
VALUES
VALUES (v_ord_id,
(v_ord_id, v_counter);
v_counter);
v_counter
v_counter :=
:= v_counter
v_counter ++ 1;1;
EXIT
EXIT WHEN
WHEN v_counter
v_counter >> 10;
10;
END
END LOOP;
LOOP;
.. .. ..
24-23

.Oracle Corporation, 1996. All rights reserved ‫ س‬Copyright


WHILE Loop: Syntax

Use the WHILE loop to repeat statements while a


condition is TRUE.
WHILE
WHILE condition
condition LOOP
LOOP Condition is
statement1 evaluated at the
statement1;;
beginning of
statement2
statement2;; each iteration.
.. .. ..
[increment_statement]
[increment_statement]
END
END LOOP;
LOOP;

25-23

.Oracle Corporation, 1996. All rights reserved ‫ س‬Copyright


While loop example
DECLARE
;i number(4):=1
begin
while(i<=10) loop
;DBMS_OUTPUT.PUT_LINE ('While loop: i = ' || TO_CHAR(i))
;i := i + 1

;END LOOP
;end

26-23

.Oracle Corporation, 1996. All rights reserved ‫ س‬Copyright


WHILE Loop: Example
Insert the first ten new line items for order number 101.

.. .. ..
v_ord_id
v_ord_id s_item.ord_id%TYPE
s_item.ord_id%TYPE :=:= 101;
101;
v_counter
v_counter NUMBER(2)
NUMBER(2) :=
:= 1;
1;
BEGIN
BEGIN
.. .. ..
WHILE
WHILE v_counter
v_counter <=
<= 10
10 LOOP
LOOP
INSERT
INSERT INTO
INTO s_item
s_item (ord_id,
(ord_id, item_id)
item_id)
VALUES
VALUES (v_ord_id,
(v_ord_id, v_counter);
v_counter);
v_counter
v_counter :=
:= v_counter
v_counter ++ 1;
1;
END
END LOOP;
LOOP;
.. .. ..
27-23

.Oracle Corporation, 1996. All rights reserved ‫ س‬Copyright


FOR Loop: Syntax

• Use a FOR loop to shortcut the test for the


number of iterations.
FOR
FOR index
index in
in rang
rang loop
loop
(( lower_bound..upper_bound
lower_bound..upper_bound LOOP)
LOOP)
statement1
statement1;;
statement2
statement2;;
.. .. ..
END
END LOOP;
LOOP;

• Do not declare the index; it is declared implicitly.

28-23

.Oracle Corporation, 1996. All rights reserved ‫ س‬Copyright


For loop Example

begin
for i in 1..10 loop
;DBMS_OUTPUT.PUT_LINE(i)
;end loop
;end

29-23

.Oracle Corporation, 1996. All rights reserved ‫ س‬Copyright


Reverse FOR Loop: Syntax

• Use a FOR loop to shortcut the test for the


number of iterations.
FOR
FOR index
index in
in [REVERSE]
[REVERSE] range
range loop
loop
(( upper_bound..lower_bound
upper_bound..lower_bound LOOP
LOOP
statement1
statement1;;
statement2
statement2;;
.. .. ..
END
END LOOP;
LOOP;

• Do not declare the index; it is declared implicitly.

30-23

.Oracle Corporation, 1996. All rights reserved ‫ س‬Copyright


Advantage of for loop

Implicit variable declaration .1


Implicit variable increment with 1 .2
Implicit exit .3

31-23

.Oracle Corporation, 1996. All rights reserved ‫ س‬Copyright


FOR Loop: Example

Example
• Print the number of times the loop is executed
and the last value for the index.
Guidelines
• Reference the index within the loop only; it is
undefined outside the loop.
• Use an expression to reference the existing value
of an index.
• Do not reference the index as the target of an
assignment.

32-23

.Oracle Corporation, 1996. All rights reserved ‫ س‬Copyright


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.

33-23

.Oracle Corporation, 1996. All rights reserved ‫ س‬Copyright


Nested Loops and Labels: Example
Exit an outer loop based on the values of an inner block.
...
...
BEGIN
BEGIN
<<Outer-loop>>
<<Outer-loop>> LOOP
LOOP
v_counter
v_counter :=v_counter+1;
:=v_counter+1;
EXIT
EXIT WHEN
WHEN v_counter>10;
v_counter>10;
<<Inner_loop>>
<<Inner_loop>> LOOP
LOOP
...
...
EXIT
EXIT Outer_loop
Outer_loop WHEN total_done == ‘‘YES
WHEN total_done YES’’;;
--Leave
--Leave both
both loops
loops
EXIT
EXIT WHEN inner_done == ‘‘YES
WHEN inner_done YES’’;;
--Leave
--Leave inner
inner loop
loop only
only
...
...
END
END LOOP
LOOP Inner_loop;
Inner_loop;
...
...
END
END LOOP
LOOP Outer_loop;
Outer_loop;
END;
END;
34-23

.Oracle Corporation, 1996. All rights reserved ‫ س‬Copyright


EXIT and CONTINUE IN FOR LOOP

begin
for i in 1..10 loop
;EXIT WHEN (i = 4)
;DBMS_OUTPUT.PUT_LINE(i)
;end loop
;end

35-23

.Oracle Corporation, 1996. All rights reserved ‫ س‬Copyright


EXIT AND CONTINUE


begin •
for i in 1..10 loop •
;CONTINUE WHEN (i = 4) •
;DBMS_OUTPUT.PUT_LINE(i) •
;end loop •
;end •

36-23

.Oracle Corporation, 1996. All rights reserved ‫ س‬Copyright


Summary

Change the logical flow of statements by using


control structures.
• Conditional (IF statement)
• Loops
– Basic loop
– FOR loop
– WHILE loop
– EXIT statement

37-23

.Oracle Corporation, 1996. All rights reserved ‫ س‬Copyright


Practice Overview

• Performing conditional actions using the IF


statement
• Performing iterative steps by using the loop
structure
• Viewing the values of variables during runtime by
using Procedure Builder debugging features

38-23

.Oracle Corporation, 1996. All rights reserved ‫ س‬Copyright

You might also like