Oracle PL/SQL - Basics of PL/SQL Programs

Introduction

The basic code unit of a PL/SQL source program is the block.

The block groups related declarations and statements together.

A PL/SQL block is defined by the keywords DECLARE, BEGIN, EXCEPTION, and END.

These keywords divide the block into

  • a declarative part,
  • an executable part, and
  • an exception-handling part.

Only the executable part is required.

A block can have a label.

The following code shows the basic structure of a PL/SQL block.

<< optional label >> 
DECLARE    -- Optional Declarative part  
           -- Declarations of local types, variables, & subprograms 

BEGIN      -- Required Executable part  
           -- Statements 

[EXCEPTION -- Optional Exception-handling part 
           -- Exception handlers for exceptions raised in executable part]
END; 

The variable declared in the Declarations are local to the block.

Blocks can be nested.

A block is an executable statement, it can appear in another block wherever an executable statement is allowed.

The block is not stored in the database. Therefore it is called an anonymous block even if it has a label.

Variables and Constants

PL/SQL lets you declare variables and constants.

You can use them wherever you can use an expression.

Related Topics