Oracle PL/SQL - PL SQL Introduction Blocks

Introduction

The simplest kind of PL/SQL code is called an anonymous block.

An anonymous block is a block of code that has its own DECLARE/BEGIN/END structure.

Anonymous blocks can either stand on their own or they can sit within any other PL/SQL program.

declare 
     ... 
      <Declaration part>
     ... 
begin 
     ... 
     <Procedural part> 
     ... 
exception 
     ... 
     <Exception handler> 
     ... 
end; 

declaration

The declaration defines all variables, cursors, subprograms, and other elements to be used.

This section is optional, and you may skip it if no variables or program elements need to be declared.

Procedural

The procedural section contains the main body of PL/SQL.

It starts with the begin keyword and ends with the exception keyword or the end keyword if you have no exception section.

This part is mandatory.

You must have at least one line of executable code in the procedural section.

If you don't have anything to execute, you can use the NULL command.

Exception

The exception section is optional.

It allows the program to intercept and process special conditions that could happen at runtime.

For example, divide by zero, duplicate value of the primary key, and so on.

Related Topic