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


Performing SQL Operations with Native Dynamic SQL

A happy and gracious flexibility ... — Matthew Arnold

This chapter shows you how to use native dynamic SQL (dynamic SQL for short), a PL/SQL interface that makes your programs more flexible, by building and processing SQL statements at run time.

With dynamic SQL, you can directly execute any kind of SQL statement (even data definition and data control statements). You can build statements where you do not know table names, WHERE clauses, and other information in advance.

This chapter contains these topics:

What Is Dynamic SQL? on page 7-1

Why Use Dynamic SQL? on page 7-2

Using the EXECUTE IMMEDIATE Statement on page 7-2

Building a Dynamic Query with Dynamic SQL on page 7-4

Using Bulk Dynamic SQL on page 7-6

Guidelines for Dynamic SQL on page 7-8

What Is Dynamic SQL?

Some programs must build and process SQL statements where some information is not known in advance. A reporting application might build different SELECT statements for the various reports it generates, substituting new table and column names and ordering or grouping by different columns. Database management applications might issue statements such as CREATE, DROP, and GRANT that cannot be coded directly in a PL/SQL program. These statements are called dynamic SQL statements.

Dynamic SQL statements built as character strings built at run time. The strings contain the text of a SQL statement or PL/SQL block. They can also contain placeholders for bind arguments. Placeholder names are prefixed by a colon, and the names themselves do not matter. For example, PL/SQL makes no distinction between the following strings:

'DELETE FROM emp WHERE sal > :my_sal AND comm < :my_comm' 'DELETE FROM emp WHERE sal > :s AND comm < :c'

Performing SQL Operations with Native Dynamic SQL 7-1

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