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

Choosing Between Native Dynamic SQL and the DBMS_SQL Package

Examples of DBMS_SQL Package Code and Native Dynamic SQL Code

The following examples illustrate the differences in the code necessary to complete operations with the DBMS_SQL package and native dynamic SQL. Specifically, the following types of examples are presented:

A query

A DML operation

A DML returning operation

In general, the native dynamic SQL code is more readable and compact, which can improve developer productivity.

Querying Using Dynamic SQL: Example

The following example includes a dynamic query statement with one bind variable (:jobname) and two select columns (ename and sal):

stmt_str := 'SELECT ename, sal FROM emp WHERE job = :jobname';

This example queries for employees with the job description SALESMAN in the job column of the emp table. Table 6–2 shows sample code that accomplishes this query using the DBMS_SQL package and native dynamic SQL.

Coding Dynamic SQL Statements 6-17

Choosing Between Native Dynamic SQL and the DBMS_SQL Package

Table 6–2 Querying Using the DBMS_SQL Package and Native Dynamic SQL

DBMS_SQL Query Operation

Native Dynamic SQL Query Operation

 

 

DECLARE

DECLARE

stmt_str varchar2(200);

TYPE EmpCurTyp IS REF CURSOR;

cur_hdl int;

cur EmpCurTyp;

rows_processed int;

stmt_str VARCHAR2(200);

name varchar2(10);

name VARCHAR2(20);

salary int;

salary NUMBER;

BEGIN

BEGIN

cur_hdl := dbms_sql.open_cursor; -- open cursor

stmt_str := 'SELECT ename, sal FROM emp

stmt_str := 'SELECT ename, sal FROM emp WHERE

WHERE job = :1';

job = :jobname';

OPEN cur FOR stmt_str USING 'SALESMAN';

dbms_sql.parse(cur_hdl, stmt_str, dbms_

 

sql.native);

LOOP

 

FETCH cur INTO name, salary;

-- supply binds (bind by name)

EXIT WHEN cur%NOTFOUND;

dbms_sql.bind_variable(

-- <process data>

cur_hdl, 'jobname', 'SALESMAN');

END LOOP;

 

CLOSE cur;

-- describe defines

END;

dbms_sql.define_column(cur_hdl, 1, name, 200);

/

dbms_sql.define_column(cur_hdl, 2, salary);

 

rows_processed := dbms_sql.execute(cur_hdl); --

 

execute

 

LOOP

 

-- fetch a row

 

IF dbms_sql.fetch_rows(cur_hdl) > 0 then

 

-- fetch columns from the row

 

dbms_sql.column_value(cur_hdl, 1, name);

 

dbms_sql.column_value(cur_hdl, 2, salary);

 

-- <process data>

 

ELSE

 

EXIT;

 

END IF;

 

END LOOP;

 

dbms_sql.close_cursor(cur_hdl); -- close cursor

 

END;

 

/

 

 

 

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

Choosing Between Native Dynamic SQL and the DBMS_SQL Package

Performing DML Using Dynamic SQL: Example

The following example includes a dynamic INSERT statement for a table with three columns:

stmt_str := 'INSERT INTO dept_new VALUES (:deptno, :dname, :loc)';

This example inserts a new row for which the column values are in the PL/SQL variables deptnumber, deptname, and location. Table 6–3 shows sample code that accomplishes this DML operation using the DBMS_SQL package and native dynamic SQL.

Table 6–3 DML Operation Using the DBMS_SQL Package and Native Dynamic SQL

DBMS_SQL DML Operation

Native Dynamic SQL DML Operation

 

 

DECLARE

DECLARE

stmt_str VARCHAR2(200);

stmt_str VARCHAR2(200);

cur_hdl NUMBER;

deptnumber NUMBER := 99;

deptnumber NUMBER := 99;

deptname VARCHAR2(20);

deptname VARCHAR2(20);

location VARCHAR2(10);

location VARCHAR2(10);

BEGIN

rows_processed NUMBER;

stmt_str := 'INSERT INTO dept_new VALUES

BEGIN

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

stmt_str := 'INSERT INTO dept_new VALUES

EXECUTE IMMEDIATE stmt_str

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

USING deptnumber, deptname, location;

cur_hdl := DBMS_SQL.OPEN_CURSOR;

END;

DBMS_SQL.PARSE(

/

cur_hdl, stmt_str, 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);

 

rows_processed := dbms_sql.execute(cur_hdl);

 

-- execute

 

DBMS_SQL.CLOSE_CURSOR(cur_hdl); -- close

 

END;

 

/

 

 

 

Performing DML with RETURNING Clause Using Dynamic SQL: Example

The following example uses a dynamic UPDATE statement to update the location of a department, then returns the name of the department:

stmt_str := 'UPDATE dept_new SET loc = :newloc

Coding Dynamic SQL Statements 6-19

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