- Write IF … THEN … ELSIF … ELSE … END IF with relational and logical operators.
- Use LOOP … END LOOP with EXIT and EXIT WHEN, nested loops and loop labels.
- Use WHILE loops and numeric FOR loops (including REVERSE).
- Use GOTO with labels and know its restrictions.
IF Statement
IF condition-1 THEN statements_set_1; [ELSIF condition-2 THEN statements_set_2;] ... [ELSE statements_set_3;] END IF;Conditions use relational operators: >, >=, <, <=, =, not equal (<>, !=, ~=, ^=), LIKE, BETWEEN … AND, IN and IS NULL, combined with AND (both true) and OR (either true).
declare
v_ns number(5);
v_fee courses.fee%type;
begin
select count(*) into v_ns from students
where bcode in ( select bcode from batches where ccode = 'ora' );
select fee into v_fee from courses where ccode = 'ora';
if v_ns > 100 then
v_fee := v_fee * 1.2; -- +20%
elsif v_ns > 50 then
v_fee := v_fee * 1.15; -- +15%
elsif v_ns > 10 then
v_fee := v_fee * 1.05; -- +5%
else
v_fee := v_fee * 0.90; -- -10%
end if;
update courses set fee = v_fee where ccode = 'ora';
commit;
end;- Conditions are checked in order; once one is true its statements run and the IF ends. ELSE runs if none is true.
- Every IF needs a matching END IF; ELSIF (spelled without an E) does not, and can appear only after an IF.
LOOP, EXIT and EXIT WHEN
LOOP … END LOOP is the simplest loop and has no termination point – without EXIT it is infinite. EXIT leaves the loop immediately; EXIT WHEN condition leaves it only when the condition is true.
declare
i number(2) := 1;
begin
loop
dbms_output.put_line(i);
i := i + 1;
exit when i > 10;
end loop;
end;1 2 3 4 5 6 7 8 9 10
Nested loops and loop labels
A loop inside another loop runs completely for each iteration of the outer loop (e.g. multiplication tables of 1–5). A loop label lets EXIT leave an enclosing loop:
<<outerloop>>
loop
loop
exit outerloop when ... ; -- leaves the OUTER loop
end loop;
end loop;WHILE Loop
WHILE condition LOOP statements; END LOOP;The condition is checked at the beginning of each iteration; statements run as long as it is true, so they may not run at all.
declare
i number(2) := 1;
begin
while i <= 10 loop
dbms_output.put_line(i);
i := i + 1;
end loop;
end;Numeric FOR Loop
FOR counter IN [REVERSE] lowerrange .. upperrange LOOP statements; END LOOP;- The counter is set to lowerrange (to upperrange with REVERSE).
- If the counter is ≤ upperrange (≥ lowerrange with REVERSE) the statements run, otherwise the loop ends.
- The counter is incremented (decremented with REVERSE) by one and only one – the step cannot be changed.
- Step 2 is repeated. The ranges may be expressions.
begin
for i in reverse 1..10 loop
dbms_output.put_line(i); -- 10 down to 1
end loop;
end;
-- missing roll numbers between the lowest and highest
declare
v_minrollno students.rollno%type;
v_maxrollno students.rollno%type;
v_count number(2);
begin
select min(rollno), max(rollno) into v_minrollno, v_maxrollno from students;
for i in v_minrollno .. v_maxrollno loop
select count(*) into v_count from students where rollno = i;
if v_count = 0 then
dbms_output.put_line(i);
end if;
end loop;
end;GOTO Statement
GOTO label transfers control to a label (<<name>>). The label must be unique and must precede an executable statement or a PL/SQL block.
GOTO cannot
- branch into an IF statement
- branch into a LOOP
- branch into a sub-block
- branch out of a subprogram (procedure / function)
- branch from an exception handler into the current block
- IF … ELSIF … ELSE … END IF chooses among several actions.
- LOOP needs EXIT; WHILE tests first; FOR steps by exactly 1 (REVERSE counts down).
- Loop labels let EXIT leave outer loops; GOTO has strict branching restrictions.
Chapter 16 Notes (PDF)
Question / Answer (15 questions)
Moderate-level questions based on the Chapter 16 notes (PDF). Click a question to see the answer.
MCQ Test : Check Your Understanding (5 questions)
Select an option and press Check Answer, or answer all questions and press Submit Test to see your score. Each answer comes with an explanation.
Bibliography/References
The chapter notes PDF is the course study material and is credited to its author below. The explanations, questions and MCQs on this page are original study aids written for this course from the topics covered in that PDF. The other works are cited for verification and further reading. Oracle, SQL*Plus and PL/SQL are trademarks of Oracle Corporation; other names belong to their respective owners.
- Srikanth, P. (n.d.). Oracle For Beginners (chapter notes – the PDF shown on each chapter page). Srikanth Technologies. http://www.srikanthtechnologies.com/
- Oracle Corporation. (n.d.). Oracle Database PL/SQL Language Reference, 19c. https://docs.oracle.com/en/database/oracle/oracle-database/19/lnpls/
- Feuerstein, S., & Pribyl, B. (2014). Oracle PL/SQL Programming (6th ed.). O’Reilly Media.