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

A Dynamic SQL Scenario Using Native Dynamic SQL

END IF; END;

/

Using native dynamic SQL, you can write a smaller, more flexible event dispatcher similar to the following:

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

BEGIN

EXECUTE IMMEDIATE 'BEGIN

EVENT_HANDLER_' || to_char(event) || '(:1); END;'

USING param; END;

/

Performing Dynamic Operations Using Invoker's Rights

By using the invoker's rights feature with dynamic SQL, you can build applications that issue dynamic SQL statements under the privileges and schema of the invoker. These two features—invoker's rights and dynamic SQL—enable you to build reusable application subcomponents that can operate on and access the invoker's data and modules.

See Also: PL/SQL User's Guide and Reference for information about using invoker's rights and native dynamic SQL

A Dynamic SQL Scenario Using Native Dynamic SQL

This scenario shows you how to perform the following operations using native dynamic SQL:

Execute DDL and DML operations

Execute single row and multiple row queries

The database in this scenario is a company's human resources database (named hr) with the following data model:

A master table named offices contains the list of all company locations. The offices table has the following definition:

Coding Dynamic SQL Statements 6-7

A Dynamic SQL Scenario Using Native Dynamic SQL

Column Name

Null?

Type

LOCATION

NOT_NULL

VARCHAR2(200)

Multiple emp_location tables contain the employee information, where location is the name of city where the office is located. For example, a table named emp_houston contains employee information for the company's Houston office, while a table named emp_boston contains employee information for the company's Boston office.

Each emp_location table has the following definition:

Column Name

Null?

Type

EMPNO

NOT_NULL

NUMBER(4)

ENAME

NOT_NULL

VARCHAR2(10)

JOB

NOT_NULL

VARCHAR2(9)

SAL

NOT_NULL

NUMBER(7,2)

DEPTNO

NOT_NULL

NUMBER(2)

The following sections describe various native dynamic SQL operations that can be performed on the data in the hr database.

Sample DML Operation Using Native Dynamic SQL

The following native dynamic SQL procedure gives a raise to all employees with a particular job title:

CREATE OR REPLACE PROCEDURE salary_raise (raise_percent NUMBER, job VARCHAR2) IS TYPE loc_array_type IS TABLE OF VARCHAR2(40)

INDEX BY binary_integer; dml_str VARCHAR2(200); loc_array loc_array_type;

BEGIN

--bulk fetch the list of office locations SELECT location BULK COLLECT INTO loc_array

FROM offices;

--for each location, give a raise to employees with the given 'job' FOR i IN loc_array.first..loc_array.last LOOP

dml_str := 'UPDATE emp_' || loc_array(i)

||' SET sal = sal * (1+(:raise_percent/100))'

||' WHERE job = :job_title';

EXECUTE IMMEDIATE dml_str USING raise_percent, job; END LOOP;

END;

/

SHOW ERRORS;

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

A Dynamic SQL Scenario Using Native Dynamic SQL

Sample DDL Operation Using Native Dynamic SQL

The EXECUTE IMMEDIATE statement can perform DDL operations. For example, the following procedure adds an office location:

CREATE OR REPLACE PROCEDURE add_location (loc VARCHAR2) IS

BEGIN

--insert new location in master table INSERT INTO offices VALUES (loc);

--create an employee information table EXECUTE IMMEDIATE

'CREATE TABLE ' || 'emp_' || loc || '(

empno

NUMBER(4) NOT NULL,

ename

VARCHAR2(10),

job

VARCHAR2(9),

sal

NUMBER(7,2),

deptno

NUMBER(2)

)';

 

END;

 

/

 

SHOW ERRORS;

 

The following procedure deletes an office location:

CREATE OR REPLACE PROCEDURE drop_location (loc VARCHAR2) IS

BEGIN

--delete the employee table for location 'loc' EXECUTE IMMEDIATE 'DROP TABLE ' || 'emp_' || loc;

--remove location from master table

DELETE FROM offices WHERE location = loc; END;

/

SHOW ERRORS;

Sample Single-Row Query Using Native Dynamic SQL

The EXECUTE IMMEDIATE statement can perform dynamic single-row queries. You can specify bind variables in the USING clause and fetch the resulting row into the target specified in the INTO clause of the statement.

The following function retrieves the number of employees at a particular location performing a specified job:

CREATE OR REPLACE FUNCTION get_num_of_employees (loc VARCHAR2, job VARCHAR2)

Coding Dynamic SQL Statements 6-9

A Dynamic SQL Scenario Using Native Dynamic SQL

RETURN NUMBER IS query_str VARCHAR2(1000); num_of_employees NUMBER;

BEGIN

query_str := 'SELECT COUNT(*) FROM '

||' emp_' || loc

||' WHERE job = :job_title'; EXECUTE IMMEDIATE query_str

INTO num_of_employees USING job;

RETURN num_of_employees;

END;

/

SHOW ERRORS;

Sample Multiple-Row Query Using Native Dynamic SQL

The OPEN-FOR, FETCH, and CLOSE statements can perform dynamic multiple-row queries. For example, the following procedure lists all of the employees with a particular job at a specified location:

CREATE OR REPLACE PROCEDURE list_employees(loc VARCHAR2, job VARCHAR2) IS TYPE cur_typ IS REF CURSOR;

c

cur_typ;

query_str

VARCHAR2(1000);

emp_name

VARCHAR2(20);

emp_num

NUMBER;

BEGIN

 

query_str := 'SELECT ename, empno FROM emp_' || loc

||' WHERE job = :job_title';

--find employees who perform the specified job OPEN c FOR query_str USING job;

LOOP

FETCH c INTO emp_name, emp_num; EXIT WHEN c%NOTFOUND;

-- process row here END LOOP;

CLOSE c;

END;

/

SHOW ERRORS;

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

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