Loops
Loops
EXIT; END IF; END LOOP; END; Example 2: Another way of writing example 1 DECLARE I NUMBER := 0; BEGIN LOOP I := I + 1; EXIT WHEN I = 100; END LOOP; END;
DECLARE I NUMBER := 1; BEGIN LOOP INSERT INTO sample VALUES(I, I); I := I * 5; EXIT WHEN I >100; END LOOP; END;
Example 4: Using for loop for a counter from 1 to 100. DECLARE a NUMBER := 1; BEGIN FOR counter IN 1 .. 100 LOOP a := a + (counter * 5); EXIT WHEN a > 10000; END LOOP; END; Example 5: In this example, the counter will start from 100 and reach 1. DECLARE a NUMBER := 1; lower_index := 1; upper_index := 100 BEGIN FOR counter IN REVERSE lower_index .. upper_index LOOP a := a + (counter * 5); END LOOP; END; WHILE Loops A quantity can be issued if the total amount is below a sanctioned amount. DECLARE
qty NUMBER := 1; sanctioned_amt NUMBER := 1000; unit_price NUMBER := 10; tot_amt NUMBER := 0; BEGIN WHILE tot_amt < sanctioned_amt LOOP tot_amt := unit_price * qty; qty := qty + 1; END LOOP; END; Following procedure searches the name of a teacher in the relation teacher
CREATE OR REPLACE PROCEDURE search_teacher (o_t_no IN NUMBER, o_f_name OUT VARCHAR2, o_l_name OUT VARCHAR2) IS BEGIN SELECT f_name, l_name FROM teacher WHERE t_no = o_t_no; END search_teacher; run; /* it is used to create the procedure */ To call this procedure: DECLARE o_f_name teacher.f_name%TYPE; o_l_name teacher.l_name%TYPE; BEGIN search_teacher( 113, o_f_name, o_l_name); DBMS_OUTPUT.PUT_LINE(Employee : 113); DBMS_OUTPUT.PUT_LINE(Name : || o_f_name || || o_l_name); END;