Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
5.97 Mб

Declaring Nested PL/SQL Subprograms

call. (Do not confuse the RETURN statement with the RETURN clause in a function spec, which specifies the datatype of the return value.)

A subprogram can contain several RETURN statements. The subprogram does not have to conclude with a RETURN statement. Executing any RETURN statement completes the subprogram immediately.

In procedures, a RETURN statement does not return a value and so cannot contain an expression. The statement returns control to the caller before the end of the procedure.

In functions, a RETURN statement must contain an expression, which is evaluated when the RETURN statement is executed. The resulting value is assigned to the function identifier, which acts like a variable of the type specified in the RETURN clause. Observe how the function balance returns the balance of a specified bank account:

FUNCTION balance (acct_id INTEGER) RETURN REAL IS acct_bal REAL;


SELECT bal INTO acct_bal FROM accts WHERE acct_no = acct_id;

RETURN acct_bal; END balance;


The following example shows that the expression in a function RETURN statement can be arbitrarily complex:

FUNCTION compound ( years NUMBER, amount NUMBER,


RETURN amount * POWER((rate / 100) + 1, years); END compound;


In a function, there must be at least one execution path that leads to a RETURN statement. Otherwise, you get a function returned without value error at run time.

Declaring Nested PL/SQL Subprograms

You can declare subprograms in any PL/SQL block, subprogram, or package. The subprograms must go at the end of the declarative section, after all other items.

You must declare a subprogram before calling it. This requirement can make it difficult to declare several nested subprograms that call each other.

You can declare interrelated nested subprograms using a forward declaration: a subprogram spec terminated by a semicolon, with no body.

Although the formal parameter list appears in the forward declaration, it must also appear in the subprogram body. You can place the subprogram body anywhere after the forward declaration, but they must appear in the same program unit.

Example 8–3 Forward Declaration for a Nested Subprogram


PROCEDURE proc1(arg_list); -- forward declaration

PROCEDURE proc2(arg_list); -- calls proc1

PROCEDURE proc1(arg_list) IS BEGIN proc2; END; -- calls proc2


Using PL/SQL Subprograms 8-5

Соседние файлы в папке Oracle 10g