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

Using Bulk Dynamic SQL

cv RefCurTyp; p Person;

h Hobbies; BEGIN

OPEN cv FOR 'SELECT pers, hobbs FROM ' || tab_name; LOOP

FETCH cv INTO p, h; EXIT WHEN cv%NOTFOUND;

-- print attributes of 'p' and elements of 'h' END LOOP;

CLOSE cv; END;

END;

/

From an anonymous block, you might call the procedures in package TEAMS:

DECLARE

team_name VARCHAR2(15); BEGIN

team_name := 'Notables'; teams.create_table(team_name); teams.insert_row(team_name, Person('John', 31),

Hobbies('skiing', 'coin collecting', 'tennis')); teams.insert_row(team_name, Person('Mary', 28),

Hobbies('golf', 'quilting', 'rock climbing')); teams.print_table(team_name);

END;

/

Using Bulk Dynamic SQL

Bulk SQL passes entire collections back and forth, not just individual elements. This technique improves performance by minimizing the number of context switches between the PL/SQL and SQL engines. You can use a single statement instead of a loop that issues a SQL statement in every iteration.

Using the following commands, clauses, and cursor attribute, your applications can construct bulk SQL statements, then execute them dynamically at run time:

BULK FETCH statement

BULK EXECUTE IMMEDIATE statement

FORALL statement

COLLECT INTO clause RETURNING INTO clause

%BULK_ROWCOUNT cursor attribute

The static versions of these statements, clauses, and cursor attribute are discussed in "Reducing Loop Overhead for DML Statements and Queries (FORALL, BULK COLLECT)" on page 11-7. Refer to that section for background information.

Using Dynamic SQL with Bulk SQL

Bulk binding lets Oracle bind a variable in a SQL statement to a collection of values. The collection type can be any PL/SQL collection type (index-by table, nested table, or varray). The collection elements must have a SQL datatype such as CHAR, DATE, or NUMBER. Three statements support dynamic bulk binds: EXECUTE IMMEDIATE,

FETCH, and FORALL.

7-6 PL/SQL User's Guide and Reference

Using Bulk Dynamic SQL

EXECUTE IMMEDIATE

You can use the BULK COLLECT INTO clause with the EXECUTE IMMEDIATE statement to store values from each column of a query's result set in a separate collection.

You can use the RETURNING BULK COLLECT INTO clause with the EXECUTE IMMEDIATE statement to store the results of an INSERT, UPDATE, or DELETE statement in a set of collections.

FETCH

You can use the BULK COLLECT INTO clause with the FETCH statement to store values from each column of a cursor in a separate collection.

FORALL

You can put an EXECUTE IMMEDIATE statement with the RETURNING BULK

COLLECT INTO inside a FORALL statement. You can store the results of all the

INSERT, UPDATE, or DELETE statements in a set of collections.

You can pass subscripted collection elements to the EXECUTE IMMEDIATE statement through the USING clause. You cannot concatenate the subscripted elements directly into the string argument to EXECUTE IMMEDIATE; for example, you cannot build a collection of table names and write a FORALL statement where each iteration applies to a different table.

Examples of Dynamic Bulk Binds

Example 7–5 Dynamic SQL with BULK COLLECT INTO Clause

You can bind define variables in a dynamic query using the BULK COLLECT INTO clause. As the following example shows, you can use that clause in a bulk FETCH or bulk EXECUTE IMMEDIATE statement:

DECLARE

TYPE EmpCurTyp IS REF CURSOR; TYPE NumList IS TABLE OF NUMBER;

TYPE NameList IS TABLE OF VARCHAR2(15); emp_cv EmpCurTyp;

empnos NumList; enames NameList; sals NumList;

BEGIN

OPEN emp_cv FOR 'SELECT empno, ename FROM emp'; FETCH emp_cv BULK COLLECT INTO empnos, enames; CLOSE emp_cv;

EXECUTE IMMEDIATE 'SELECT sal FROM emp' BULK COLLECT INTO sals;

END;

/

Example 7–6 Dynamic SQL with RETURNING BULK COLLECT INTO Clause

Only INSERT, UPDATE, and DELETE statements can have output bind variables. You bulk-bind them with the RETURNING BULK COLLECT INTO clause of EXECUTE IMMEDIATE:

DECLARE

Performing SQL Operations with Native Dynamic SQL 7-7

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