Control Flow in PL/SQL
PL/SQL allows you to branch and create loops in a fairly familiar way.
An IF
statement looks like:
IF <condition> THEN <statement_list>
ELSE <statement_list>
END IF;
The ELSE part is optional.
If you want a multiway branch, use:
IF <condition_1> THEN ...
ELSIF <condition_2> THEN ...
... ...
ELSIF <condition_n> THEN ...
ELSE ...
END IF;
The following is an example, slightly modified from the previous one, where now we only do the insertion if the second component is 1.
If not, we first add 10 to each component and then insert:
SQL> DECLARE
2 a NUMBER;
3 b NUMBER;
4 BEGIN
5 SELECT e, f INTO a, b FROM T1 WHERE e > 1;
6 IF b = 1 THEN
7 INSERT INTO T1 VALUES( b, a );
8 ELSE
9 INSERT INTO T1 VALUES( b+10, a+10 );
10 END IF;
11 END;
12 /
PL/SQL procedure successfully completed.
|
|
Loops are created with the following:
LOOP
<loop_body> /* a list of statements */
END LOOP;