Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Semestr2 / 1 - Oracle / Oracle selected docs / Database concepts.pdf
Скачиваний:
29
Добавлен:
12.05.2015
Размер:
6.96 Mб
Скачать

PL/SQL Overview

various stages of compilation. Outline data is stored in the OUTLN schema. You can tune execution plans by editing stored outlines.

Editing Stored Outlines The outline is cloned into the user’s schema at the onset of the outline editing session. All subsequent editing operations are performed on that clone until the user is satisfied with the edits and chooses to publicize them. In this way, any editing done by the user does not impact the rest of the user community, which would continue to use the public version of the outline until the edits are explicitly saved.

See Also: Oracle9i Database Performance Tuning Guide and Reference for details about execution plans and using stored outlines

PL/SQL Overview

PL/SQL is Oracle’s procedural language extension to SQL. PL/SQL enables you to mix SQL statements with procedural constructs. With PL/SQL, you can define and run PL/SQL program units such as procedures, functions, and packages.

PL/SQL program units generally are categorized as anonymous blocks and stored procedures.

An anonymous block is a PL/SQL block that appears within your application and it is not named or stored in the database. In many applications, PL/SQL blocks can appear wherever SQL statements can appear.

A stored procedure is a PL/SQL block that Oracle stores in the database and can be called by name from an application. When you create a stored procedure, Oracle parses the procedure and stores its parsed representation in the database. Oracle also lets you create and store functions (which are similar to procedures) and packages (which are groups of procedures and functions).

See Also:

"Java Overview" on page 14-31

Chapter 17, "Triggers"

14-16 Oracle9i Database Concepts

PL/SQL Overview

How PL/SQL Runs

Native Execution

For best performance on computationally intensive program units, compile the source code of PL/SQL program units stored in the database directly to object code for the given platform. (This object code is linked into the Oracle server.)

See Also: PL/SQL User’s Guide and Reference

Interpreted Execution

In versions earlier than Oracle9i, PL/SQL source code was always compiled into a so-called bytecode representation, which is executed by a portable virtual machine implemented as part of the Oracle Server, and also in products such as Oracle Forms. Starting with Oracle9i, you can choose between native execution and interpreted execution

The PL/SQL engine is the tool you use to define, compile, and run PL/SQL program units. This engine is a special component of many Oracle products, including the Oracle server.

While many Oracle products have PL/SQL components, this section specifically covers the program units that can be stored in an Oracle database and processed using the Oracle server PL/SQL engine. The PL/SQL capabilities of each Oracle tool are described in the appropriate tool's documentation.

Figure 14–2 illustrates the PL/SQL engine contained in Oracle server.

SQL, PL/SQL, and Java 14-17

PL/SQL Overview

Figure 14–2 The PL/SQL Engine and the Oracle Server

Oracle Server

Database

SGA

PL/SQL Engine

Application

 

 

Program code

Procedure

Procedural

 

Statement

Program code

Begin

Executor

Procedural

 

Prodedure call

Procedural

 

SQL

 

Program code

Prodedural

SQL

SQL

Program code

END;

 

 

 

 

 

SQL Statement

 

 

Executor

Database

The program unit is stored in a database. When an application calls a procedure stored in the database, Oracle loads the compiled program unit into the shared pool in the system global area (SGA). The PL/SQL and SQL statement executors work together to process the statements within the procedure.

The following Oracle products contain a PL/SQL engine:

Oracle server

Oracle Forms (version 3 and later)

SQL*Menu (version 5 and later)

Oracle Reports (version 2 and later)

Oracle Graphics version 2 and later)

14-18 Oracle9i Database Concepts

PL/SQL Overview

You can call a stored procedure from another PL/SQL block, which can be either an anonymous block or another stored procedure. For example, you can call a stored procedure from Oracle Forms (version 3 or later).

Also, you can pass anonymous blocks to Oracle from applications developed with these tools:

Oracle precompilers (including user exits)

Oracle Call Interfaces (OCIs)

SQL*Plus

Oracle Enterprise Manager

Language Constructs for PL/SQL

PL/SQL blocks can include the following PL/SQL language constructs:

Variables and constants

Cursors

Exceptions

This section gives a general description of each construct.

See Also: PL/SQL User’s Guide and Reference

Variables and Constants

Variables and constants can be declared within a procedure, function, or package. A variable or constant can be used in a SQL or PL/SQL statement to capture or provide a value when one is needed.

Note: Some interactive tools, such as SQL*Plus, let you define variables in your current session. You can use such variables just as you would variables declared within procedures or packages.

Cursors

Cursors can be declared explicitly within a procedure, function, or package to facilitate record-oriented processing of Oracle data. Cursors also can be declared implicitly (to support other data manipulation actions) by the PL/SQL engine.

See Also: "Scrollable Cursors" on page 14-7

SQL, PL/SQL, and Java 14-19

PL/SQL Overview

Exceptions

PL/SQL lets you explicitly handle internal and user-defined error conditions, called exceptions, that arise during processing of PL/SQL code. Internal exceptions are caused by illegal operations, such as division by zero, or Oracle errors returned to the PL/SQL code. User-defined exceptions are explicitly defined and signaled within the PL/SQL block to control processing of errors specific to the application (for example, debiting an account and leaving a negative balance).

When an exception is raised, the execution of the PL/SQL code stops, and a routine called an exception handler is invoked. Specific exception handlers can be written for any internal or user-defined exception.

Dynamic SQL in PL/SQL

PL/SQL can run dynamic SQL statements whose complete text is not known until runtime. Dynamic SQL statements are stored in character strings that are entered into, or built by, the program at runtime. This enables you to create general purpose procedures. For example, dynamic SQL lets you create a procedure that operates on a table whose name is not known until runtime.

You can write stored procedures and anonymous PL/SQL blocks that include dynamic SQL in two ways:

By embedding dynamic SQL statements in the PL/SQL block

By using the DBMS_SQL package

Additionally, you can issue DML or DDL statements using dynamic SQL. This helps solve the problem of not being able to statically embed DDL statements in PL/SQL. For example, you can choose to issue a DROP TABLE statement from within a stored procedure by using the EXECUTE IMMEDIATE statement or the PARSE procedure supplied with the DBMS_SQL package.

See Also:

Oracle9i Application Developer’s Guide - Fundamentals for a comparison of the two approaches to dynamic SQL

PL/SQL User’s Guide and Reference for details about dynamic SQL

Oracle9i Supplied PL/SQL Packages and Types Reference

14-20 Oracle9i Database Concepts

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