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

Building a Dynamic Query with Dynamic SQL

condition IN VARCHAR2 DEFAULT NULL) AS

where_clause VARCHAR2(100) := ' WHERE ' || condition; BEGIN

IF condition IS NULL THEN where_clause := NULL; END IF; EXECUTE IMMEDIATE 'DELETE FROM ' || table_name || where_clause;

END;

/

Specifying Parameter Modes for Bind Variables in Dynamic SQL Strings

With the USING clause, the mode defaults to IN, so you do not need to specify a parameter mode for input bind arguments.

With the RETURNING INTO clause, the mode is OUT, so you cannot specify a parameter mode for output bind arguments.

You must specify the parameter mode in more complicated cases, such as this one where you call a procedure from a dynamic PL/SQL block:

CREATE PROCEDURE create_dept (

deptno

IN OUT NUMBER,

dname

IN VARCHAR2,

loc

IN VARCHAR2) AS

BEGIN

SELECT deptno_seq.NEXTVAL INTO deptno FROM dual; INSERT INTO dept VALUES (deptno, dname, loc);

END;

/

To call the procedure from a dynamic PL/SQL block, you must specify the IN OUT mode for the bind argument associated with formal parameter deptno, as follows:

DECLARE

plsql_block VARCHAR2(500);

new_deptno

NUMBER(2);

new_dname

VARCHAR2(14) := 'ADVERTISING';

new_loc

VARCHAR2(13) := 'NEW YORK';

BEGIN

 

plsql_block := 'BEGIN create_dept(:a, :b, :c); END;'; EXECUTE IMMEDIATE plsql_block

USING IN OUT new_deptno, new_dname, new_loc; IF new_deptno > 90 THEN ...

END;

/

Building a Dynamic Query with Dynamic SQL

You use three statements to process a dynamic multi-row query: OPEN-FOR, FETCH, and CLOSE. First, you OPEN a cursor variable FOR a multi-row query. Then, you FETCH rows from the result set one at a time. When all the rows are processed, you CLOSE the cursor variable. (For more information about cursor variables, see "Using Cursor Variables (REF CURSORs)" on page 6-19.)

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

Examples of Dynamic SQL for Records, Objects, and Collections

Examples of Dynamic SQL for Records, Objects, and Collections

Example 7–3 Dynamic SQL Fetching into a Record

As the following example shows, you can fetch rows from the result set of a dynamic multi-row query into a record:

DECLARE

TYPE EmpCurTyp IS REF CURSOR;

emp_cv

EmpCurTyp;

emp_rec

emp%ROWTYPE;

sql_stmt

VARCHAR2(200);

my_job

VARCHAR2(15) := 'CLERK';

BEGIN

 

sql_stmt

:= 'SELECT * FROM emp WHERE job = :j';

OPEN emp_cv FOR sql_stmt USING my_job;

LOOP

 

FETCH

emp_cv INTO emp_rec;

EXIT WHEN emp_cv%NOTFOUND; -- process record

END LOOP; CLOSE emp_cv;

END;

/

Example 7–4 Dynamic SQL for Object Types and Collections

The next example illustrates the use of objects and collections. Suppose you define object type Person and VARRAY type Hobbies, as follows:

CREATE TYPE Person AS OBJECT (name VARCHAR2(25), age NUMBER);

CREATE TYPE Hobbies IS VARRAY(10) OF VARCHAR2(25);

Using dynamic SQL, you can write a package that uses these types:

CREATE OR REPLACE PACKAGE teams AS

PROCEDURE create_table (tab_name VARCHAR2);

PROCEDURE insert_row (tab_name VARCHAR2, p Person, h Hobbies); PROCEDURE print_table (tab_name VARCHAR2);

END;

/

CREATE OR REPLACE PACKAGE BODY teams AS PROCEDURE create_table (tab_name VARCHAR2) IS BEGIN

EXECUTE IMMEDIATE 'CREATE TABLE ' || tab_name || ' (pers Person, hobbs Hobbies)';

END;

PROCEDURE insert_row ( tab_name VARCHAR2, p Person,

h Hobbies) IS BEGIN

EXECUTE IMMEDIATE 'INSERT INTO ' || tab_name || ' VALUES (:1, :2)' USING p, h;

END;

PROCEDURE print_table (tab_name VARCHAR2) IS

TYPE RefCurTyp IS REF CURSOR;

Performing SQL Operations with Native Dynamic SQL 7-5

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