Block structure

PL/SQL programs are divided up into structures(blocks). Each block contains PL/SQL and SQL statements. A PL/SQL block has the following structure:


[DECLARE
declaration_statements
]
BEGIN
executable_statements
[EXCEPTION
exception_handling_statements
]
END;
/

It says:

ItemDescription
declaration_statementsoptional, declares the variables used in the rest of the PL/SQL block.
executable_statementsthe actual executable statements.
exception_handling_statementsoptional, handle any execution errors.

Every statement is terminated by a semicolon (;). A PL/SQL block is terminated using the forward slash (/) character.


SQL> SET SERVEROUTPUT ON
SQL>
SQL> DECLARE
  2   v_width INTEGER := 3;
  3   v_height INTEGER := 2;
  4   v_area INTEGER;
  5  BEGIN
  6   v_area := v_width *  v_height;
  7   v_width := v_area /  v_height;
  8   DBMS_OUTPUT.PUT_LINE('area = ' || v_area);
  9   DBMS_OUTPUT.PUT_LINE('width = ' || v_width);
 10  EXCEPTION
 11   WHEN ZERO_DIVIDE THEN
 12   DBMS_OUTPUT.PUT_LINE('Division by zero');
 13  END;
 14  /
area = 6
width = 3

PL/SQL procedure successfully completed.

SQL>

The SET SERVEROUTPUT ON command turns the server output on.

Home »
Oracle »
PL/SQL » 

Related: