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

Choosing Between Native Dynamic SQL and the DBMS_SQL Package

Choosing Between Native Dynamic SQL and the DBMS_SQL Package

Oracle Database provides two methods for using dynamic SQL within PL/SQL: native dynamic SQL and the DBMS_SQL package. Native dynamic SQL lets you place dynamic SQL statements directly into PL/SQL code. These dynamic statements include DML statements (including queries), PL/SQL anonymous blocks, DDL statements, transaction control statements, and session control statements.

To process most native dynamic SQL statements, you use the EXECUTE IMMEDIATE statement. To process a multi-row query (SELECT statement), you use OPEN-FOR, FETCH, and CLOSE statements.

Note: To use native dynamic SQL, the COMPATIBLE initialization parameter must be set to 8.1.0 or higher. See Oracle Database Upgrade Guide for more information about the COMPATIBLE parameter.

The DBMS_SQL package is a PL/SQL library that offers an API to execute SQL statements dynamically. The DBMS_SQL package has procedures to open a cursor, parse a cursor, supply binds, and so on. Programs that use the DBMS_SQL package make calls to this package to perform dynamic SQL operations.

The following sections provide detailed information about the advantages of both methods.

See Also:

The PL/SQL User's Guide and Reference for detailed information about using native dynamic SQL. Native dynamic SQL is referred to in that book as "dynamic SQL".

PL/SQL Packages and Types Reference for detailed information about using the DBMS_SQL package

Advantages of Native Dynamic SQL

Native dynamic SQL provides the following advantages over the DBMS_SQL package:

Coding Dynamic SQL Statements 6-11

Choosing Between Native Dynamic SQL and the DBMS_SQL Package

Native Dynamic SQL is Easy to Use

Because native dynamic SQL is integrated with SQL, you can use it in the same way that you use static SQL within PL/SQL code. Native dynamic SQL code is typically more compact and readable than equivalent code that uses the DBMS_SQL package.

With the DBMS_SQL package you must call many procedures and functions in a strict sequence, making even simple operations require a lot of code. You can avoid this complexity by using native dynamic SQL instead.

Table 6–1 illustrates the difference in same operation using the DBMS_SQL

the amount of code required to perform the package and native dynamic SQL.

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

Choosing Between Native Dynamic SQL and the DBMS_SQL Package

Table 6–1 Code Comparison of DBMS_SQL Package and Native Dynamic SQL

DBMS_SQL Package

 

Native Dynamic SQL

 

 

 

CREATE PROCEDURE insert_into_table (

CREATE PROCEDURE insert_into_table (

table_name

VARCHAR2,

table_name

VARCHAR2,

deptnumber

NUMBER,

deptnumber

NUMBER,

deptname

VARCHAR2,

deptname

 

VARCHAR2,

location

VARCHAR2) IS

location

 

VARCHAR2) IS

cur_hdl

INTEGER;

stmt_str

VARCHAR2(200);

stmt_str

VARCHAR2(200);

 

 

 

rows_processed

BINARY_INTEGER;

BEGIN

 

 

 

 

stmt_str := 'INSERT INTO ' ||

BEGIN

 

table_name || ' values

stmt_str := 'INSERT INTO ' ||

(:deptno, :dname, :loc)';

table_name || ' VALUES

 

 

 

(:deptno, :dname, :loc)';

EXECUTE IMMEDIATE stmt_str

 

 

USING

 

 

-- open cursor

 

deptnumber, deptname, location;

cur_hdl := dbms_sql.open_cursor;

 

 

 

 

 

END;

 

 

-- parse cursor

 

/

 

 

dbms_sql.parse(cur_hdl, stmt_str,

SHOW ERRORS;

 

 

dbms_sql.native);

 

 

 

-- supply binds

 

 

 

 

dbms_sql.bind_variable

 

 

 

(cur_hdl, ':deptno', deptnumber);

 

 

 

dbms_sql.bind_variable

 

 

 

(cur_hdl, ':dname', deptname);

 

 

 

dbms_sql.bind_variable

 

 

 

(cur_hdl, ':loc', location);

 

 

 

-- execute cursor

 

 

 

rows_processed :=

 

 

 

dbms_sql.execute(cur_hdl);

 

 

 

-- close cursor

 

 

 

dbms_sql.close_cursor(cur_hdl);

 

 

 

END;

 

 

 

 

/

 

 

 

 

SHOW ERRORS;

 

 

 

 

 

 

 

 

 

Coding Dynamic SQL Statements 6-13

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