PL/SQL Control Statements

There are three types of loop supported by PLSQL

  • SIMPLE Loop
  • WHILE Loop
  • FOR Loop

SIMPLE LOOP:

This loop is also called as infinite loop, here body of the loop statements is executed repeatedly

Below is the Syntax:

LOOP

Executable statements;

END LOOP;

Write a program to print message by using loop, Example with syntax

BEGIN

LOOP

Dbms_output.put_line(‘Welcome’);

END LOOP;

END;

  • Output: ORA-2000: ORA-10027: buffer overflow, limit of 2000 bytes.

To exit from infinite loop oracle has provided two methods, below are the syntax

Method1:

DECLARE

A Number(10):=1;

BEGIN

LOOP

A:=A+1;

Dbms_output.put_line(A);

EXIT WHEN A>=10;

END LOOP;

END;

Syntax:

EXIT WHEN TRUECONDITION;

Method2(Using IF) Syntax

IF TRUECONDITION THEN

EXIT;

END IF;

Another syntax to exit from the loop:

DECLARE

A Number(10) := 1;

BEGIN

LOOP

A := A+1;

Dbms_output.put_line(A);

IF A >= 10 THEN

EXIT;

END IF;

END LOOP;

END;