Previous |
Next |
A subprogram follows PL/SQL block structure; that is, it has:
The declarative part contains declarations of types, constants, variables, exceptions, explicit cursors, and nested subprograms. These items are local to the subprogram and cease to exist when the subprogram completes execution.
The executable part contains statements that assign values, control execution, and manipulate data.
Exception-handling part (optional)
The exception-handling part contains code that handles exceptions (run-time errors).
Comments can appear anywhere in PL/SQL code. The PL/SQL compiler ignores them. Adding comments to your program promotes readability and aids understanding. An inline comment starts with a double hyphen (--
) and extends to the end of the line. A multiline comment starts with a slash and asterisk (/*
) and ends with an asterisk and a slash (*/
).
The structure of a procedure is:
PROCEDURE name [ ( parameter_list ) ] { IS | AS } [ declarative_part ] BEGIN -- executable part begins statement; [ statement; ]... [ EXCEPTION -- executable part ends, exception-handling part begins] exception_handler; [ exception_handler; ]... ] END; /* exception-handling part ends if it exists; otherwise, executable part ends */
The structure of a function is like that of a procedure, except that it includes a RETURN
clause and at least one RETURN
statement (and some optional clauses that are beyond the scope of this document):
FUNCTION name [ ( parameter_list ) ] RETURN data_type
[ clauses ]
{ IS | AS }
[ declarative_part ]
BEGIN -- executable part begins
-- at least one statement must be a RETURN statement
statement; [ statement; ]...
[ EXCEPTION -- executable part ends, exception-handling part begins]
exception_handler; [ exception_handler; ]... ]
END; /* exception-handling part ends if it exists;
otherwise, executable part ends */
The code that begins with PROCEDURE
or FUNCTION
and ends before IS
or AS
is the subprogram signature. The declarative, executable, and exception-handling parts comprise the subprogram body. The syntax of exception-handler
is in "About Exceptions and Exception Handlers".