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

Cursor Variables

Declaring and Opening Cursor Variables

Memory is usually allocated for a cursor variable in the client application using the appropriate ALLOCATE statement. In Pro*C, use the EXEC SQL ALLOCATE <cursor_name> statement. In OCI, use the Cursor Data Area.

You can also use cursor variables in applications that run entirely in a single server session. You can declare cursor variables in PL/SQL subprograms, open them, and use them as parameters for other PL/SQL subprograms.

Examples of Cursor Variables

This section includes several examples of cursor variable usage in PL/SQL. For additional cursor variable examples that use the programmatic interfaces, see the following manuals:

Pro*C/C++ Programmer's Guide

Pro*COBOL Programmer's Guide

Oracle Call Interface Programmer's Guide

Oracle SQL*Module for Ada Programmer's Guide

Fetching Data

The following package defines a PL/SQL cursor variable type Emp_val_cv_type, and two procedures. The first procedure, Open_emp_cv, opens the cursor variable using a bind variable in the WHERE clause. The second procedure, Fetch_emp_ data, fetches rows from the Emp_tab table using the cursor variable.

CREATE OR REPLACE PACKAGE Emp_data AS

TYPE Emp_val_cv_type IS REF CURSOR RETURN Emp_tab%ROWTYPE;

PROCEDURE Open_emp_cv (Emp_cv

IN OUT

Emp_val_cv_type,

Dept_number

IN

INTEGER);

PROCEDURE Fetch_emp_data (emp_cv

IN

Emp_val_cv_type,

emp_row

OUT

Emp_tab%ROWTYPE);

END Emp_data;

 

 

CREATE OR REPLACE PACKAGE BODY Emp_data AS

 

PROCEDURE Open_emp_cv (Emp_cv

IN OUT Emp_val_cv_type,

Dept_number IN

INTEGER) IS

BEGIN

 

 

OPEN emp_cv FOR SELECT * FROM Emp_tab WHERE deptno = dept_number;

END open_emp_cv;

 

 

PROCEDURE Fetch_emp_data (Emp_cv

IN

Emp_val_cv_type,

Using Procedures and Packages 7-31

Cursor Variables

Emp_row

OUT Emp_tab%ROWTYPE) IS

BEGIN

 

FETCH Emp_cv INTO Emp_row;

 

END Fetch_emp_data;

 

END Emp_data;

 

The following example shows how to call the Emp_data package procedures from a PL/SQL block:

DECLARE

--declare a cursor variable Emp_curs Emp_data.Emp_val_cv_type; Dept_number Dept_tab.Deptno%TYPE; Emp_row Emp_tab%ROWTYPE;

BEGIN

Dept_number := 20;

--open the cursor using a variable Emp_data.Open_emp_cv(Emp_curs, Dept_number);

--fetch the data and display it LOOP

Emp_data.Fetch_emp_data(Emp_curs, Emp_row); EXIT WHEN Emp_curs%NOTFOUND; DBMS_OUTPUT.PUT(Emp_row.Ename || ' '); DBMS_OUTPUT.PUT_LINE(Emp_row.Sal);

END LOOP;

END;

Implementing Variant Records

The power of cursor variables comes from their ability to point to different cursors. In the following package example, a discriminant is used to open a cursor variable to point to one of two different cursors:

CREATE OR REPLACE

PACKAGE Emp_dept_data AS

TYPE Cv_type IS

REF CURSOR;

 

PROCEDURE Open_cv (Cv

IN

OUT cv_type,

 

Discrim

IN

POSITIVE);

END Emp_dept_data;

 

 

 

CREATE OR REPLACE

PACKAGE BODY Emp_dept_data AS

PROCEDURE Open_cv (Cv

IN OUT

cv_type,

 

Discrim IN

POSITIVE) IS

BEGIN

IF Discrim = 1 THEN

OPEN Cv FOR SELECT * FROM Emp_tab WHERE Sal > 2000;

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

Тут вы можете оставить комментарий к выбранному абзацу или сообщить об ошибке.

Оставленные комментарии видны всем.

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