Tuesday, 17 October 2017

PL/SQL Block Structure

Here we will understand the basic PL/SQL block structure. The PL/SQL block structure consist of four sections as shown below.

DECLARE (Optional): The declarative sections begins with the keyword DECLARE and ends with the executable section (BEGIN) starts. It contains declaration of all variables, constants, cursors, and user-defined exceptions that are referenced in the executable sections and the exception sections.

BEGIN (Mandatory): The executable section begins with the keyword BEGIN. This section needs to have at least one statement. However, the executable section of a PL/SQL block can include any number of PL/SQL blocks. It contains SQL statements to retrieve data from the database; contains PL/SQL statements to manipulate data in the block.

EXCEPTION (Optional): The exception section is nested within the executable section. This section begins with the keyword EXCEPTION. It specifies the actions to perform when errors and abnormal conditions arise in the executable section.

END (Mandatory): All PL/SQL blocks must conclude with an END statement. Observe that END is terminated with a semicolon.

Note: In a PL/SQL block, the keywords DECLARE, BEGIN, and EXCEPTION are not terminated by a semicolon. However, the keyword END, all SQL statements, and PL/SQL statements must be terminated with a semicolon.