SPL block structure overview v16
Regardless of whether the program is a procedure, function, subprogram, or trigger, an SPL program has the same block structure. A block consists of up to three sections: an optional declaration section, a mandatory executable section, and an optional exception section. Minimally, a block has an executable section that consists of one or more SPL statements between the keywords
Use the optional declaration section to declare variables, cursors, types, and subprograms that are used by the statements in the executable and exception sections. Declarations appear just before the
BEGIN keyword of the executable section. Depending on the context of where the block is used, the declaration section can begin with the keyword
You can include an exception section in the
BEGIN - END block. The exception section begins with the keyword
EXCEPTION and continues until the end of the block in which it appears. If an exception is thrown by a statement in the block, program control might go to the exception section where the thrown exception is handled, depending on the exception and the contents of the exception section.
The following is the general structure of a block:
pragmasare the directives (
AUTONOMOUS_TRANSACTIONis the currently supported pragma).
declarationsare one or more variable, cursor, type, or subprogram declarations that are local to the block. If subprogram declarations are included, you must declare them after all other variable, cursor, and type declarations. Terminate each declaration with a semicolon. The use of the keyword
DECLAREdepends on the context in which the block appears.
statementsare one or more SPL statements. Terminate each statement with a semicolon. You must also terminate the end of the block denoted by the keyword
ENDwith a semicolon.
- If present, the keyword
EXCEPTIONmarks the beginning of the exception section.
exception_conditionis a conditional expression testing for one or more types of exceptions. If an exception matches one of the exceptions in
WHEN exception_conditionclause are executed. There can be one or more
WHEN exception_conditionclauses, each followed by
BEGIN/END block is considered a statement, thus you can nest blocks. The exception section can also contain nested blocks.
The following is the simplest possible block, consisting of the
NULL statement in the executable section. The
NULL statement is an executable statement that does nothing.
The following block contains a declaration section as well as the executable section:
In this example, three numeric variables are declared of data type
NUMBER. Values are assigned to two of the variables, and one number is divided by the other. Results are stored in a third variable and then displayed. The output is:
The following block consists of a declaration, an executable, and an exception:
The following output shows that the statement in the exception section is executed as a result of the division by zero: