29 December, 2015

Oracle | Forward Declaration

Forward Declaration of Elements

There can be cases where the elements have to called in program code before they are coded in the program. But Oracle is very fuzzy about the declaration of program units. It will throw you an error because if the element is not defined, Oracle treats it as wrong semantic.

To resolve cases, where mutual recursion is present, meaning a program element calls another program unit which in turn calls the first program unit, this issue can be resolved using forward declarations.
In forward declaration, the actual element is not coded, but rather a declaration of it is made before calling it in another program unit. This ensures that Oracle knows the code for the declared program unit is somewhere later present in the code.

There are few guidelines for forward declaration as well:
1. A variable or a cursor cannot be forward declared.
2. Only modules like a procedure or a function can be forward declared.
3. The definition of the forward declared element should be present in the declaration part of the same PL/SQL block where the element is declared.