Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
40
Добавлен:
16.04.2013
Размер:
4.96 Mб
Скачать

Overview of PL/SQL Program Units

Is stored in the data dictionary.

Can be called by many users.

Note: The term stored procedure is sometimes used generically for both stored procedures and stored functions. The only difference between procedures and functions is that functions always return a single value to the caller, while procedures do not return a value to the caller.

Naming Procedures and Functions

Because a procedure or function is stored in the database, it must be named. This distinguishes it from other stored procedures and makes it possible for applications to call it. Each publicly-visible procedure or function in a schema must have a unique name, and the name must be a legal PL/SQL identifier.

Note: If you plan to call a stored procedure using a stub generated by SQL*Module, then the stored procedure name must also be a legal identifier in the calling host 3GL language, such as Ada or C.

Parameters for Procedures and Functions

Stored procedures and functions can take parameters. The following example shows a stored procedure that is similar to the anonymous block in "Anonymous Blocks" on page 7-2.

Caution: To execute the following, use CREATE OR REPLACE

PROCEDURE...

PROCEDURE Get_emp_names (Dept_num IN NUMBER) IS

Emp_name

VARCHAR2(10);

CURSOR

c1 (Depno NUMBER) IS

 

SELECT Ename FROM Emp_tab

 

WHERE deptno = Depno;

BEGIN

OPEN c1(Dept_num);

LOOP

FETCH c1 INTO Emp_name;

EXIT WHEN C1%NOTFOUND;

Using Procedures and Packages 7-5

Overview of PL/SQL Program Units

DBMS_OUTPUT.PUT_LINE(Emp_name);

END LOOP;

CLOSE c1;

END;

In this stored procedure example, the department number is an input parameter which is used when the parameterized cursor c1 is opened.

The formal parameters of a procedure have three major attributes, described in Table 7–1.

Table 7–1 Attributes of Procedure Parameters

Parameter Attribute

Description

 

 

Name

This must be a legal PL/SQL identifier.

Mode

This indicates whether the parameter is an input-only

 

parameter (IN), an output-only parameter (OUT), or is

 

both an input and an output parameter (IN OUT). If the

 

mode is not specified, then IN is assumed.

Datatype

This is a standard PL/SQL datatype.

 

 

Parameter Modes Parameter modes define the behavior of formal parameters. The three parameter modes, IN (the default), OUT, and IN OUT, can be used with any subprogram. However, avoid using the OUT and IN OUT modes with functions. The purpose of a function is to take no arguments and return a single value. It is poor programming practice to have a function return multiple values. Also, functions should be free from side effects, which change the values of variables not local to the subprogram.

Table 7–2 summarizes the information about parameter modes.

Table 7–2 Parameter Modes

IN

OUT

IN OUT

 

 

 

The default.

Must be specified.

Must be specified.

Passes values to a

Returns values to the caller.

Passes initial values to a

subprogram.

 

subprogram; returns

 

 

updated values to the

 

 

caller.

Formal parameter acts like

Formal parameter acts like

Formal parameter acts like

a constant.

an uninitialized variable.

an initialized variable.

7-6 Oracle Database Application Developer's Guide - Fundamentals

 

 

 

Overview of PL/SQL Program Units

 

 

 

 

 

Table 7–2 (Cont.) Parameter Modes

 

 

 

 

 

 

 

 

IN

OUT

IN OUT

 

 

 

 

 

 

Formal parameter cannot

Formal parameter cannot

Formal parameter should

 

be assigned a value.

be used in an expression;

be assigned a value.

 

 

must be assigned a value.

 

 

 

Actual parameter can be a

Actual parameter must be

Actual parameter must be

 

constant, initialized

a variable.

a variable.

 

variable, literal, or

 

 

 

 

expression.

 

 

 

 

 

 

 

 

See Also: PL/SQL User's Guide and Reference for details about parameter modes

Parameter Datatypes The datatype of a formal parameter consists of one of the following:

An unconstrained type name, such as NUMBER or VARCHAR2.

A type that is constrained using the %TYPE or %ROWTYPE attributes.

Note: Numerically constrained types such as NUMBER(2) or

VARCHAR2(20) are not allowed in a parameter list.

%TYPE and %ROWTYPE Attributes Use the type attributes %TYPE and %ROWTYPE to constrain the parameter. For example, the Get_emp_names procedure specification in "Parameters for Procedures and Functions" on page 7-5 could be written as the following:

PROCEDURE Get_emp_names(Dept_num IN Emp_tab.Deptno%TYPE)

This has the Dept_num parameter take the same datatype as the Deptno column in the Emp_tab table. The column and table must be available when a declaration using %TYPE (or %ROWTYPE) is elaborated.

Using %TYPE is recommended, because if the type of the column in the table changes, then it is not necessary to change the application code.

If the Get_emp_names procedure is part of a package, then you can use previously-declared public (package) variables to constrain a parameter datatype. For example:

Dept_number number(2);

Using Procedures and Packages 7-7

Overview of PL/SQL Program Units

...

PROCEDURE Get_emp_names(Dept_num IN Dept_number%TYPE);

Use the %ROWTYPE attribute to create a record that contains all the columns of the specified table. The following example defines the Get_emp_rec procedure, which returns all the columns of the Emp_tab table in a PL/SQL record for the given empno:

Caution: To execute the following, use CREATE OR REPLACE

PROCEDURE...

PROCEDURE Get_emp_rec (Emp_number

IN Emp_tab.Empno%TYPE,

Emp_ret

OUT Emp_tab%ROWTYPE) IS

BEGIN

SELECT Empno, Ename, Job, Mgr, Hiredate, Sal, Comm, Deptno

INTO Emp_ret

FROM Emp_tab

WHERE Empno = Emp_number;

END;

You could call this procedure from a PL/SQL block as follows:

DECLARE

 

 

 

Emp_row

Emp_tab%ROWTYPE;

-- declare a record matching a

 

 

-- row in the Emp_tab table

BEGIN

 

 

 

Get_emp_rec(7499, Emp_row);

-- call for Emp_tab# 7499

DBMS_OUTPUT.PUT(Emp_row.Ename

|| ' '

|| Emp_row.Empno);

DBMS_OUTPUT.PUT(' '

|| Emp_row.Job || ' ' || Emp_row.Mgr);

DBMS_OUTPUT.PUT(' '

|| Emp_row.Hiredate

|| ' ' || Emp_row.Sal);

DBMS_OUTPUT.PUT(' '

|| Emp_row.Comm || ' '|| Emp_row.Deptno);

DBMS_OUTPUT.NEW_LINE;

 

 

END;

 

 

 

Stored functions can also return values that are declared using %ROWTYPE. For example:

FUNCTION Get_emp_rec (Dept_num IN Emp_tab.Deptno%TYPE)

RETURN Emp_tab%ROWTYPE IS ...

Tables and Records You can pass PL/SQL tables as parameters to stored procedures and functions. You can also pass tables of records as parameters.

7-8 Oracle Database Application Developer's Guide - Fundamentals

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