Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Programming PL SQL.doc
Скачиваний:
3
Добавлен:
01.07.2025
Размер:
5.06 Mб
Скачать

Chapter 15. Dynamic sql and Dynamic pl/sql

Dynamic SQL refers to SQL statements that are constructed and executed at runtime. Dynamic is the opposite of static. Static SQL refers to SQL statements that are fixed at the time a program is compiled. Dynamic PL/SQL refers to entire PL/SQL blocks of code that are constructed dynamically, then compiled and executed.

Time for a confession: I have had more fun writing dynamic SQL and dynamic PL/SQL programs than just about anything else I have ever done with the PL/SQL language. By constructing and executing dynamically, you gain a tremendous amount of flexibility. You can also build extremely generic and widely useful reusable code.

So what can you do with dynamic SQL and dynamic PL/SQL?[1] Here are just a few ideas:

[1] For the remainder of this chapter, any reference to "dynamic SQL" also includes dynamic PL/SQL blocks, unless otherwise stated.

Execute DDL statements

You can only execute queries and DML statements with static SQL inside PL/SQL. What if you want to create a table or drop an index? Time for dynamic SQL!

Build back-ends for web-based applications

These might allow users to specify which columns they want to see and vary the order in which they see the data. In other words, you can support full ad hoc querying and updating applications.

Write a generic string parsing engine

Such a parsing engine might accept a delimited list and deposit the elements of that list into your collection.

Ever since Oracle Version 7.1, we PL/SQL developers have been able to use the built-in DBMS_SQL package to execute dynamic SQL. In Oracle8i, we were given a second option for executing dynamically constructed SQL statements: native dynamic SQL (NDS). The DBMS_SQL package is examined in depth in the book Oracle Built-in Packages (described in Chapter 1). In this chapter, we'll concentrate on NDS, which is easier to write and faster to execute. Also, unlike DBMS_SQL, NDS is a native part of the PL/SQL language, so it makes sense to cover it here. There are times, however, when you will want to use DBMS_SQL. You will find at the end of this chapter a comparison of the two approaches and recommendations for selecting the implementation path for your dynamic SQL needs.

15.1 Nds Statements

One of the nicest things about NDS is its simplicity. Unlike DBMS_SQL, which has dozens of programs and lots of rules to follow, NDS has been integrated into the PL/SQL language by adding one new statement, EXECUTE IMMEDIATE, which executes a specified SQL statement immediately, and by enhancing the existing OPEN FOR statement, which allows you to perform multiple-row dynamic queries.

The EXECUTE IMMEDIATE and OPEN FOR statements will not be directly accessible from Oracle Forms Builder and Oracle Reports Builder until the PL/SQL version in those tools are upgraded to Oracle8i or Oracle9i. Prior to that, you will need to create stored programs that hide calls to these constructs; you will then be able to execute those stored programs from within your client-side PL/SQL code.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]