0% found this document useful (0 votes)
5 views3 pages

PL/SQL Looping Techniques Explained

This document discusses various examples of using loops in PL/SQL including: 1. A basic for loop that counts from 1 to 5 2. Nested for loops with an outer loop counting from 1 to 2 and an inner loop counting from 1 to 4 3. A for loop counting backwards from 5 to 1 4. A for loop that only outputs even numbers by checking if the counter is evenly divisible by 2 5. A for loop example using the EXIT command to break out of the loop once a condition is met.

Uploaded by

Fendy Christian
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

PL/SQL Looping Techniques Explained

This document discusses various examples of using loops in PL/SQL including: 1. A basic for loop that counts from 1 to 5 2. Nested for loops with an outer loop counting from 1 to 2 and an inner loop counting from 1 to 4 3. A for loop counting backwards from 5 to 1 4. A for loop that only outputs even numbers by checking if the counter is evenly divisible by 2 5. A for loop example using the EXIT command to break out of the loop once a condition is met.

Uploaded by

Fendy Christian
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

PLSQL

FOR
1. Perulangan Menaik

SQL>
SQL> --  Your first FOR loop.
SQL> set echo on
SQL> BEGIN
  2       FOR i IN 1..5 LOOP
  3            DBMS_OUTPUT.PUT_LINE('Loop counter is ' || i);
  4       END LOOP;
  5  END;
  6  /
Loop counter is 1
Loop counter is 2
Loop counter is 3
Loop counter is 4
Loop counter is 5

PL/SQL procedure successfully completed.

2. Nested Loop

SQL>
SQL> -- Nesting FOR loops.
SQL> BEGIN
  2       FOR i IN 1..2 LOOP
  3            FOR j IN 1..4 LOOP
  4                 DBMS_OUTPUT.PUT_LINE('Outer Loop counter is ' ||
  5                      i || ' Inner Loop counter is ' || j);
  6            END LOOP;
  7       END LOOP;
  8  END;
  9  /
Outer Loop counter is 1 Inner Loop counter is 1
Outer Loop counter is 1 Inner Loop counter is 2
Outer Loop counter is 1 Inner Loop counter is 3
Outer Loop counter is 1 Inner Loop counter is 4
Outer Loop counter is 2 Inner Loop counter is 1
Outer Loop counter is 2 Inner Loop counter is 2
Outer Loop counter is 2 Inner Loop counter is 3
Outer Loop counter is 2 Inner Loop counter is 4

PL/SQL procedure successfully completed.

SQL>
SQL>
           
         
3. Perulangan Menurun

SQL>
SQL> -- Reversing the loop.
SQL> DECLARE
  2       loop_start Integer := 1;
  3  BEGIN
  4       FOR i IN REVERSE loop_start..5 LOOP
  5            DBMS_OUTPUT.PUT_LINE('Loop counter is ' || i);
  6       END LOOP;
  7  END;
  8  /
Loop counter is 5
Loop counter is 4
Loop counter is 3
Loop counter is 2
Loop counter is 1

PL/SQL procedure successfully completed.

SQL>

4. Merubah Nilai loop menggunakan MOD 2=0

SQL>
SQL> -- Changing the loop increment.
SQL> BEGIN
  2       FOR i IN 1..6 LOOP
  3            IF MOD(i,2) = 0 THEN
  4                 DBMS_OUTPUT.PUT_LINE('Loop counter is ' || i);
  5            END IF;
  6       END LOOP;
  7  END;
  8  /
Loop counter is 2
Loop counter is 4
Loop counter is 6

PL/SQL procedure successfully completed.

SQL>
SQL>
           

5. Menggunakan Perintah EXIT untuk break pada perulangan

SQL>
SQL>
SQL> --EXIT WHEN statement
SQL>
SQL> SET SERVEROUTPUT ON
SQL> DECLARE
  2     myValue INTEGER := 5;
  3  BEGIN
  4     FOR i IN 1..12 LOOP
  5
  6          myValue := myValue +5;
  7          DBMS_OUTPUT.PUT_LINE(myValue);
  8          EXIT WHEN myValue > 100;
  9     END LOOP;
 10  END;
 11
 12
 13  /
10
15
20
25
30
35
40
45
50
55
60
65

PL/SQL procedure successfully completed.

SQL>
           

You might also like