You can nest a block within another block wherever you can place an executable statement. For more information, see "Block Structure" and "Scope and Visibility" .
[<<label_name>>] [DECLARE object_declaration [object_declaration] ... [subprogram_declaration [subprogram_declaration] ...]] BEGIN seq_of_statements [EXCEPTION exception_handler [exception_handler] ...] END [label_name];
object_declaration ::=
{ constant_declaration | cursor_declaration | cursor_variable_declaration | exception_declaration | plsql_table_declaration | record_declaration | variable_declaration}
subprogram_declaration ::=
{function_declaration | procedure_declaration}
A global identifier declared in an enclosing block can be redeclared in a sub-block, in which case the local declaration prevails and the sub-block cannot reference the global identifier. To reference the global identifier, you must use a block label to qualify the reference, as the following example shows:
<<outer>> DECLARE x INTEGER; BEGIN ... DECLARE x INTEGER; BEGIN ... IF x = outer.x THEN -- refers to global x ... END IF; END; END outer;
PL/SQL does not allow forward references. So, you must declare an object before referencing it in other statements, including other declarative statements. Also, you must declare subprograms at the end of a declarative section after all other program objects.
seq_of_statements ::=
statement [statement] ...
Statements are used to create algorithms. Besides SQL statements, PL/SQL has flow-of-control and error-handling statements. PL/SQL statements are free format. That is, they can continue from line to line, providing you do not split keywords, delimiters, or literals across lines. A semicolon (;) must terminate every PL/SQL statement. The syntax of statement follows:
statement ::=
[<<label_name>>] { assignment_statement | exit_statement | goto_statement | if_statement | loop_statement | null_statement | plsql_block | raise_statement | return_statement | sql_statement}
PL/SQL supports a subset of SQL statements that includes data manipulation, cursor control, and transaction control statements but excludes data definition and data control statements such as ALTER, CREATE, GRANT, and REVOKE. The syntax of sql_statement follows:
sql_statement ::=
{ close_statement | commit_statement | delete_statement | fetch_statement | insert_statement | lock_table_statement | open_statement | open-for_statement | rollback_statement | savepoint_statement | select_statement | set_transaction_statement | update_statement}
If there is no exception handler for the raised exception in the current block, control passes to the enclosing block. This process repeats until an exception handler is found or there are no more enclosing blocks. If PL/SQL can find no exception handler for the exception, execution stops and an unhandled exception error is returned to the host environment. For more information, see Chapter 6.
END does not signal the end of a transaction. Just as a block can span multiple transactions, a transaction can span multiple blocks.
-- available online in file EXAMP11 DECLARE numerator NUMBER; denominator NUMBER; the_ratio NUMBER; lower_limit CONSTANT NUMBER := 0.72; samp_num CONSTANT NUMBER := 132; BEGIN SELECT x, y INTO numerator, denominator FROM result_table WHERE sample_id = samp_num; the_ratio := numerator/denominator; IF the_ratio > lower_limit THEN INSERT INTO ratio VALUES (samp_num, the_ratio); ELSE INSERT INTO ratio VALUES (samp_num, -1); END IF; COMMIT; EXCEPTION WHEN ZERO_DIVIDE THEN INSERT INTO ratio VALUES (samp_num, 0); COMMIT; WHEN OTHERS THEN ROLLBACK; END;