Introduction to PL/SQL LOOP Statement - LearnHowToCode SarkariResult.com Interview Questions and Answers LearnHowToCodeOnline
PLSQL Loops

Introduction to PL/SQL LOOP Statement

PLSQL Loops in this tutorial, you will learn how to use PL/SQL LOOP statement to execute a sequence of statements repeatedly.

Introduction to PL/SQL LOOP Statement

Loops:- Like other programming languages we can use loops in PLSQL programming. There are 3 types of loop in PLSQL.

1) Simple Loop
2) While Loop
3) For Loop

Simple Loop:-

Syntax:-

loop
executable statement;
exit when ;
end loop;
 
loop
executable statement;
if then
exit;
end if;
end loop;
Note:- Exit condition is mandatory in Simple Loop otherwise program will run in infinite loop.

Example:-

declare
i number:=1;
begin
loop
dbms_output.put_line(i);
i:=i+1;
exit when i>10;
end loop;
end;
/

While Loop:-
 
Syntax:-
 
while (condition)
loop
executable statement;
end loop;
 
Example:-

declare
i number:=1;
begin
while (i<=10)
loop
dbms_output.put_line(i);
i:=i+1;
end loop;
end;
/

For Loop:- It is the most popular loop in PLSQL as a variable used for "for loop" that does not need to declare and there is no need to do manual increment.
 
Syntax:-
 
for i in start_number..end_number
loop
executable statement;
end loop;
 
Note:- Start_number should be less than end_number otherwise program will never print any result.
 
Example :-
begin
for i in 1..10 loop
dbms_output.put_line(i);
end loop;
end;
/

Reverse For Loop:- For decrement, we use "reverse" keyword.

begin
for i in REVERSE 1..10 loop
dbms_output.put_line(i);
end loop;
end;
/

About Mariano

I'm Ethan Mariano a software engineer by profession and reader/writter by passion.I have good understanding and knowledge of AngularJS, Database, javascript, web development, digital marketing and exploring other technologies related to Software development.

0 comments:

Featured post

Political Full Forms List

Acronym Full Form MLA Member of Legislative Assembly RSS Really Simple Syndication, Rashtriya Swayamsevak Sangh UNESCO United Nations E...

Powered by Blogger.