A Simple PL/SQL Block

A Simple PL/SQL Block:

Each PL/SQL program consists of SQL and PL/SQL statements which from a PL/SQL block.

A PL/SQL Block consists of three sections:

  • The Declaration section (optional).
  • The Execution section (mandatory).
  • The Exception Handling section (optional).

Declaration Section:

  • The Declaration section of a PL/SQL Block is an optional and it starts with the reserved keyword DECLARE.
  • It is used to declare any placeholders like variables, constants, records and cursors, which are used to manipulate data in the execution section.
  • Placeholders may be any of Variables, Constants and Records, which stores data temporarily. Cursors are also declared in this section.

Execution Section:

  • This section starts with the reserved keyword BEGIN and ends with END.
  • This is a mandatory section
  • This is the section where the program logic is written to perform any task.
  • The programmatic constructs like loops, conditional statement and SQL statements form the part of execution section.

Exception Section:

  • The Exception section of a PL/SQL Block starts with the reserved keyword EXCEPTION.
  • This section is optional.
  • Any errors in the program can be handled in this section, so that the PL/SQL Blocks terminates gracefully.
  • If the PL/SQL Block contains exceptions that cannot be handled, the Block terminates abruptly with errors.

Common for All Sections:

  • Every statement in the above three sections must end with a semicolon ;.
  • PL/SQL blocks can be nested within other PL/SQL blocks.
  • Comments can be used to document code.

This is how a sample PL/SQL Block looks.

DECLARE

Variable declaration

BEGIN

Program Execution

EXCEPTION

Exception handling

END;

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License