EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials PL/SQL Tutorial Loops in PL/SQL
Secondary Sidebar
Java NIO Scatter/Gather

Java 11

Java NIO File

Bootstrap 4 Datepicker

Java Project Maven

Java NIO Path

Loops in PL/SQL

By Priya PedamkarPriya Pedamkar

Loops-in-PL/SQL

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:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

different types of loop in PL SQL

  • 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.

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,268 ratings)

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”.

Syntax 

LOOP
The sequence of statements;
END LOOP;

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.

Example of Simple loop

Let’s write a program to print the multiplication table of 18.

Loops in PL SQL

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:

Loops in PL SQL 2

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.

Output:

Loops in PL_SQL 3

2. The FOR Loop

FOR loop allows you to execute the block of statements repeatedly for a fixed number of times.

Syntax

FOR loop_counter IN [REVERSE] lower_limit .. upper_limit LOOP
Statement1;
Statement2;
....Statement3;
END 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.

Example #1 

keywords FOR marks the beginning

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.

Output:

keywords FOR marks the beginning output

Example #2: Now let’s print the same in reverse order using FOR loop.

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.

reverse order using FOR loop output

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.

Syntax

WHILE condition LOOP
Statement 1;
Statement 2;
...
Statement N;
END 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.

Print multiplication table

  • 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.

Output:

Print multiplication table output

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 bSQe 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.

Recommended Articles

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–

  1. What is PL/SQL?
  2. MySQL Client
  3. MySQL REGEXP
  4. Loops in Shell Scripting
Popular Course in this category
PL SQL Training (4 Courses, 2+ Projects)
  4 Online Courses |  2 Hands-on Projects |  17+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more