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

Why Use Dynamic SQL?

Why Use Dynamic SQL?

You should use dynamic SQL in cases where static SQL does not support the operation you want to perform, or in cases where you do not know the exact SQL statements that must be executed by a PL/SQL procedure. These SQL statements may depend on user input, or they may depend on processing work done by the program. The following sections describe typical situations where you should use dynamic SQL and typical problems that can be solved by using dynamic SQL

Executing DDL and SCL Statements in PL/SQL

In PL/SQL, you can only execute the following types of statements using dynamic SQL, rather than static SQL:

Data definition language (DDL) statements, such as CREATE, DROP, GRANT, and

REVOKE

Session control language (SCL) statements, such as ALTER SESSION and

SET ROLE

See Also: Oracle Database SQL Reference for information about

DDL and SCL statements

Also, you can only use the TABLE clause in the SELECT statement through dynamic SQL. For example, the following PL/SQL block contains a SELECT statement that uses the TABLE clause and native dynamic SQL:

CREATE TYPE t_emp AS OBJECT (id NUMBER, name VARCHAR2(20))

/

CREATE TYPE t_emplist AS TABLE OF t_emp

/

CREATE TABLE dept_new (id NUMBER, emps t_emplist)

NESTED TABLE emps STORE AS emp_table;

INSERT INTO dept_new VALUES ( 10,

t_emplist(

t_emp(1, 'SCOTT'), t_emp(2, 'BRUCE')));

DECLARE

deptid NUMBER; ename VARCHAR2(20);

Coding Dynamic SQL Statements 6-3

Why Use Dynamic SQL?

BEGIN

EXECUTE IMMEDIATE 'SELECT d.id, e.name

FROM dept_new d, TABLE(d.emps) e -- not allowed in static SQL -- in PL/SQL

WHERE e.id = 1' INTO deptid, ename;

END;

/

Executing Dynamic Queries

You can use dynamic SQL to create applications that execute dynamic queries, whose full text is not known until runtime. Many types of applications need to use dynamic queries, including:

Applications that allow users to input or choose query search or sorting criteria at runtime

Applications that allow users to input or choose optimizer hints at run time

Applications that query a database where the data definitions of tables are constantly changing

Applications that query a database where new tables are created often

For examples, see "Querying Using Dynamic SQL: Example" on page 6-17, and see the query examples in "A Dynamic SQL Scenario Using Native Dynamic SQL" on page 6-7.

Referencing Database Objects that Do Not Exist at Compilation

Many types of applications must interact with data that is generated periodically. For example, you might know the tables definitions at compile time, but not the names of the tables.

Dynamic SQL can solve this problem, because it lets you wait until runtime to specify the table names. For example, in the sample data warehouse application discussed in "What Is Dynamic SQL?" on page 6-2, new tables are generated every quarter, and these tables always have the same definition. You might let a user specify the name of the table at runtime with a dynamic SQL query similar to the following:

CREATE OR REPLACE PROCEDURE query_invoice( month VARCHAR2,

year VARCHAR2) IS

TYPE cur_typ IS REF CURSOR;

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

Why Use Dynamic SQL?

c cur_typ;

query_str VARCHAR2(200); inv_num NUMBER; inv_cust VARCHAR2(20); inv_amt NUMBER;

BEGIN

query_str := 'SELECT num, cust, amt FROM inv_' || month ||'_'|| year || ' WHERE invnum = :id';

OPEN c FOR query_str USING inv_num; LOOP

FETCH c INTO inv_num, inv_cust, inv_amt; EXIT WHEN c%NOTFOUND;

-- process row here END LOOP;

CLOSE c; END;

/

Optimizing Execution Dynamically

You can use dynamic SQL to build a SQL statement in a way that optimizes the execution by concatenating the hints into a SQL statement dynamically. This lets you change the hints based on your current database statistics, without requiring recompilation.

For example, the following procedure uses a variable called a_hint to allow users to pass a hint option to the SELECT statement:

CREATE OR REPLACE PROCEDURE query_emp (a_hint VARCHAR2) AS

TYPE cur_typ IS REF CURSOR; c cur_typ;

BEGIN

OPEN c FOR 'SELECT ' || a_hint ||

' empno, ename, sal, job FROM emp WHERE empno = 7566'; -- process

END;

/

In this example, the user can pass any of the following values for a_hint:

a_hint = '/*+ ALL_ROWS */' a_hint = '/*+ FIRST_ROWS */' a_hint = '/*+ CHOOSE */'

Coding Dynamic SQL Statements 6-5

Why Use Dynamic SQL?

or any other valid hint option.

See Also: Oracle Database Performance Tuning Guide for more information about using hints

Executing Dynamic PL/SQL Blocks

You can use the EXECUTE IMMEDIATE statement to execute anonymous PL/SQL blocks. You can add flexibility by constructing the block contents at runtime.

For example, suppose you want to write an application that takes an event number and dispatches to a handler for the event. The name of the handler is in the form EVENT_HANDLER_event_num, where event_num is the number of the event. One approach is to implement the dispatcher as a switch statement, where the code handles each event by making a static call to its appropriate handler. This code is not very extensible because the dispatcher code must be updated whenever a handler for a new event is added.

CREATE OR REPLACE PROCEDURE event_handler_1(param number) AS BEGIN -- process event

RETURN;

END;

/

CREATE OR REPLACE PROCEDURE event_handler_2(param number) AS BEGIN -- process event

RETURN;

END;

/

CREATE OR REPLACE PROCEDURE event_handler_3(param number) AS BEGIN -- process event

RETURN;

END;

/

CREATE OR REPLACE PROCEDURE event_dispatcher (event number, param number) IS

BEGIN

IF (event = 1) THEN EVENT_HANDLER_1(param);

ELSIF (event = 2) THEN EVENT_HANDLER_2(param);

ELSIF (event = 3) THEN EVENT_HANDLER_3(param);

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

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