0% found this document useful (0 votes)
21 views

Loops

The document provides examples of using loops and procedures in PL/SQL. It shows FOR loops counting from 1 to 100 and in reverse, a WHILE loop incrementing a total amount until it reaches a limit, and a procedure that searches a teacher table and returns name fields matching an ID number.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Loops

The document provides examples of using loops and procedures in PL/SQL. It shows FOR loops counting from 1 to 100 and in reverse, a WHILE loop incrementing a total amount until it reaches a limit, and a procedure that searches a teacher table and returns name fields matching an ID number.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

Example 1: A loop that will execute 100 times may be: DECLARE i NUMBER := 0; BEGIN LOOP i := i + 1; IF i = 100 THEN

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;

Example 3: This example shows insertion of values in sample table.

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;

You might also like