Introduction to Loops in PL/SQL
Procedural Language/Structured Query Language or PL/SQL is Oracle Corporation’s procedural extension for the Oracle RDBMS. PL/SQL extended SQL by adding constructs used in procedural languages to enable more complex programming than SQL provides. Examples of these structures are IF…THEN…ELSE, basic loops, FOR loops, and WHILE loops.
Explain Different Types of Loops in PL/SQL
This article will explain you the iterative control structure means loops of PL/SQL; it will let you run the same code repeatedly. PL/SQL provides three different kinds of loop types:
- The simple or infinite loop
- The FOR loop
- The WHILE loop
Here, each loop is designed for a specific purpose, rules for use, and guidelines for high-quality creation.
Examples of Different Loops
Consider the following three procedures to understand different loops and their problem-solving ability in different ways.
1. The Simple Loop
This loop is as simple as its name. It starts with the LOOP keyword and ends with the end statement “END LOOP”.
The sequence of statements;
Here, as per the above syntax keyword, ‘LOOP’ marks the starting of the loop and ‘END LOOP’ stated the end of the loop. The sequence of statement part can contain any statement for execution.
4.5 (2,413 ratings)
Example of Simple loop
Let’s write a program to print the multiplication table of 18.
Here in the above loop, we do not have the “EXIT” statement; means the output execution will go on to infinite until we close this program manually.
Refer below program with Exit statement:
Explanation of the above program
In the declaration section, we have declared two variables; variable v_counter will serve as a counter and v_result will hold the result of the multiplication.
Down the execution section, we have our simple loop, here we have three statements.
- The first statement will work as our update statement; this will update our counter and increment it by 1.
- The second statement is an arithmetic expression, which will perform the multiplication of our table and will store the result in v_result variable.
- The third statement is an output statement, which will print the result of the multiplication in a formatted manner.
Use of Exit Statement
As per exit statement if v_counter >=10 then loop with an exit which means the loop will execute 10 times.
2. The FOR Loop
FOR loop allows you to execute the block of statements repeatedly for a fixed number of times.
FOR loop_counter IN [REVERSE] lower_limit .. upper_limit LOOP
- The first line of the syntax is the loop statement where keywords FOR marks the beginning of loop followed by loop counter which is an implicit index integer variable.
- This means you do not need to define this variable in the declaration section, also it will increment itself by 1 implicitly on each iteration of your loop, unlike the other loops where we have to define loop counter.
- Keyword IN is a must to be in the FOR Loop program.
- Keyword REVERSE is not mandatory but always used in conjunction with Keyword IN.
- If keyword REVERSE is used then the loop will iterate in the reverse order.
- lower_limit and upper_limit are two integer numbers. These two variables define a number of iteration of the loop.
- Two dots between these two variables serve as the range operator.
- Then we have the body of the loop, which can be a statement or group of statements.
- In the end, we have the phrase END LOOP that indicates the ending of the loop.
Here as per the above program, we have our FOR loop which will print the value of the v_counter variable from 11 to 20.
Example #2: Now let’s print the same in reverse order using FOR loop.
Just add keyword REVERSE after IN and before 11, this will execute the same o/p but in reverse order.
3. The WHILE Loop
While loop executes statements of program multiple times also this is best used for the program when no of iterations is unknown.
WHILE condition LOOP
- Unlike another syntax WHILE loop, the syntax is very easy to understand. Here as per the above syntax, ‘WHILE’ marks beginning of the loop along with condition and ‘END LOOP’ stated the end of the loop.
- The statements 1 through N are the executable statements, defined in the body of the loop. In addition, in the end, we have mentioned the END LOOP which indicates the end of the while loop.
- In order to run statements inside the body of the While loop, the condition needs to be true.
Example: Print multiplication table of 17 using while loop.
- In this example, we have the first variable “v_counter” that will serve as counter and the second variable is “v_result” this will hold the result of the multiplication.
- Here the first statement is an arithmetic expression inside WHILE loop, which will be doing the task of table multiplication and result, will get stored in v_result.
- The second statement is the print statement, which will print multiplication results. The third statement is update counter, which will update the counter with each iteration
- This while loop will keep on working until we have counter value more than or equal to 10 and WHILE loop will get terminated post 10 counter value.
Advantages of Loops in PL/SQL
- Code Re-usability is the best advantage of loops, we do not need to write code repeatedly for each iteration, using loops we can re-use code in every iteration.
- Loops also help us in reducing the code size or program size. All we have to do is just write one simple code and put that inside any loop to complete the work without coding for different outputs from the same program.
- Complexity reduction has also added the advantage of loops.
Conclusion – Loops in PL/SQL
SQL is the only interface to a relational database, and PL/SQL is a procedural extension to SQL. It is important to understand how SQL works and to design databases and business logic correctly to get the right result set. PL/SQL can be used inside the database, and it has many powerful features. There are many improvements to PL/SQL in Oracle Database 12.1. Use SQL whenever possible, but if your query gets too complicated or procedural features are needed, it is best to use PL/SQL instead.
This has been a guide to the Loops in PL/SQL. Here we also discuss advantages and different types of loops with examples. You may also have a look at the following articles to learn more–