Using Bulk Dynamic SQL. Oracle Release 2 (9.2)
Реклама
Реклама
Using Bulk Dynamic SQL
Using Bulk Dynamic SQL
In this section, you learn how to add the power of bulk binding to dynamic SQL.
Bulk binding improves performance by minimizing the number of context switches between the PL/SQL and SQL engines. With bulk binding, entire collections, not just individual elements, are passed back and forth.
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 Collections with Bulk Binds" on page 5-37. Refer to
that section for background information.
Syntax for Dynamic Bulk Binds
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). However, 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
.
Bulk EXECUTE IMMEDIATE
This statement lets you bulk-bind define variables or
OUT bind arguments passed as parameters to a dynamic SQL statement. The syntax follows:
EXECUTE IMMEDIATE dynamic_string
[[BULK COLLECT] INTO define_variable[, define_variable ...]]
[USING bind_argument[, bind_argument ...]]
[{RETURNING | RETURN}
BULK COLLECT INTO bind_argument[, bind_argument ...]];
With a dynamic multi-row query, you can use the
BULK COLLECT INTO
clause to bind define variables. The values in each column are stored in a collection.
Native Dynamic SQL
11-11
Using Bulk Dynamic SQL
With a dynamic
INSERT
,
UPDATE
, or
DELETE
statement that returns multiple rows, you can use the
RETURNING BULK COLLECT INTO
clause to bulk-bind output variables. The returned rows of values are stored in a set of collections.
Bulk FETCH
This statement lets you fetch from a dynamic cursor the same way you fetch from a static cursor. The syntax follows:
FETCH dynamic_cursor
BULK COLLECT INTO define_variable[, define_variable ...];
If the number of define variables in the
BULK COLLECT INTO
list exceeds the number of columns in the query select-list, Oracle generates an error.
Bulk FORALL
This statement lets you bulk-bind input variables in a dynamic SQL statement. In addition, you can use the
EXECUTE IMMEDIATE
statement inside a
FORALL
loop.
The syntax follows:
FORALL index IN lower bound..upper bound
EXECUTE IMMEDIATE dynamic_string
USING bind_argument | bind_argument(index)
[, bind_argument | bind_argument(index)] ...
[{RETURNING | RETURN} BULK COLLECT
INTO bind_argument[, bind_argument ... ]];
The dynamic string must represent an
INSERT
,
UPDATE
, or
DELETE
statement (not a
SELECT
statement).
Examples of Dynamic Bulk Binds
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;
11-12
PL/SQL User’s Guide and Reference
Using Bulk Dynamic SQL
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;
Only the
INSERT
,
UPDATE
, and
DELETE statements can have output bind variables.
To bulk-bind them, you use the
BULK RETURNING INTO
clause, which can appear only in an
EXECUTE IMMEDIATE
. An example follows:
DECLARE
TYPE NameList IS TABLE OF VARCHAR2(15);
enames NameList;
bonus_amt NUMBER := 500;
sql_stmt VARCHAR(200);
BEGIN
sql_stmt := ’UPDATE emp SET bonus = :1 RETURNING ename INTO :2’;
EXECUTE IMMEDIATE sql_stmt
USING bonus_amt RETURNING BULK COLLECT INTO enames;
END;
To bind the input variables in a SQL statement, you can use the
FORALL
statement and
USING
clause, as shown below. However, the SQL statement cannot be a query.
DECLARE
TYPE NumList IS TABLE OF NUMBER;
TYPE NameList IS TABLE OF VARCHAR2(15);
empnos NumList;
enames NameList;
BEGIN
empnos := NumList(1,2,3,4,5);
FORALL i IN 1..5
EXECUTE IMMEDIATE
’UPDATE emp SET sal = sal * 1.1 WHERE empno = :1
RETURNING ename INTO :2’
USING empnos(i) RETURNING BULK COLLECT INTO enames;
...
END;
Native Dynamic SQL
11-13
Изтегляне
Реклама