Open In App

PL/SQL Cursor FOR LOOP

Last Updated : 05 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The PL/SQL FOR LOOP appears as a reliable ally, reducing the difficulties of recurring activities. This article walks you through the syntax, applications, and benefits of the FOR LOOP, using concrete examples to demonstrate its diverse possibilities within PL/SQL blocks. As we continue on this journey, we hope to provide developers with a better knowledge of how the FOR LOOP may streamline operations, increasing efficiency and clarity in creating powerful database procedures and routines. Join us on this trip to discover your true potential and improve your PL/SQL skills.

PL/SQL Cursor FOR LOOP

In the realm of PL/SQL, the FOR LOOP stands as a stalwart, simplifying the complexities of repetitive tasks with elegance. This article takes a deep dive into the intricacies of the FOR LOOP in PL/SQL, unraveling its syntax, usage, and the myriad benefits it brings to the table. Through two illuminating examples, complete with code snippets and detailed output explanations, we’ll showcase the remarkable versatility of this construct.

The FOR LOOP in PL/SQL is purpose-built for seamless iteration, whether traversing a range of values or cycling through collection elements. The fundamental syntax is elegantly straightforward:

FOR loop_index IN [REVERSE] lower_bound..upper_bound

LOOP

— Statements to be executed in each iteration

END LOOP;

  • loop_index: The loop index or counter variable.
  • lower_bound and upper_bound: The range of values for the loop index.
  • REVERSE (optional): Allows looping in reverse order.

Process:

  • Initialize the loop index to the lower bound.
  • Execute the statements within the loop.
  • Increment or decrement the loop index.
  • Repeat the process until the loop index reaches the upper bound.

Syntax:

For a basic FOR LOOP:

— Basic FOR LOOP

FOR loop_index IN lower_bound..upper_bound

LOOP

— Statements to be executed in each iteration

END LOOP;

For a FOR LOOP in reverse:

— FOR LOOP in Reverse

FOR loop_index IN REVERSE lower_bound..upper_bound

LOOP

— Statements to be executed in each iteration

END LOOP;

Importance of PL/SQL Cursor FOR LOOP

  1. Efficient Iteration Over Ranges: Navigating through PL/SQL development, the FOR LOOP emerges as a linchpin, ensuring efficient iteration over specific value ranges.
  2. Streamlined Handling of Collections: The FOR LOOP extends its utility seamlessly to the realm of collections, offering a streamlined approach to handle arrays, nested tables, and other PL/SQL collections.
  3. Dynamic and Readable Code: Beyond its functional prowess, the FOR LOOP champions dynamic and readable code.
  4. Versatility in Database Procedures: It significantly boosts the efficiency of repetitive tasks within PL/SQL blocks, proving indispensable for database developers engaged in diverse data manipulation and processing operations.

Example of PL/SQL Cursor FOR LOOP

Through two illuminating examples, complete with code snippets and detailed output explanations, we’ll showcase the remarkable versatility of this construct.

Example 1: Using FOR LOOP to Print Numbers.

The realm of the FOR LOOP, where this robust construct transforms into a dependable companion for effortlessly printing sequential numbers.

-- Using FOR LOOP to Print Numbers
DECLARE
-- Loop index
loop_index NUMBER := 1;
BEGIN
FOR loop_index IN 1..5
LOOP
DBMS_OUTPUT.PUT_LINE('Number: ' || loop_index);
END LOOP;
END;
/

Output:

Number: 1
Number: 2
Number: 3
Number: 4
Number: 5

In this example, the FOR LOOP effortlessly prints the numbers from 1 to 5 using the DBMS_OUTPUT.PUT_LINE statement. The loop index dynamically changes in each iteration, providing a concise and readable solution for printing sequential numbers.

Example 2: Using FOR LOOP to Update Records.

the intricacies of how the loop index dynamically influences specific departments, showcasing the FOR LOOP’s inherent prowess in efficiently managing targeted updates.

-- Using FOR LOOP to Update Records
DECLARE
-- Loop index
loop_index NUMBER := 1;
BEGIN
FOR loop_index IN 1..3
LOOP
UPDATE employees
SET salary = salary * 1.1
WHERE department_id = loop_index;
END LOOP;
COMMIT;
END;
/

Output Explanation:

This example updates employees’ salaries in three different departments by multiplying their current salaries by 1.1. The loop index dynamically determines the specific departments being updated. The COMMIT statement finalizes the changes.

Conclusion

So, Using the FOR LOOP in PL/SQL is a versatile and convenient construct for handling iterative tasks. Whether you need to perform actions a specific number of times or iterate over elements in a collection, the FOR LOOP provides a clean and readable solution. By understanding its syntax and incorporating it into your PL/SQL code, you can enhance the efficiency and clarity of your database procedures and routines. The examples presented demonstrate the simplicity and effectiveness of the FOR LOOP in practical scenarios.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads