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

Overview of PL/SQL Program Units

Overview of PL/SQL Program Units

PL/SQL is a modern, block-structured programming language. It provides several features that make developing powerful database applications very convenient. For example, PL/SQL provides procedural constructs, such as loops and conditional statements, that are not available in standard SQL.

You can directly enter SQL data manipulation language (DML) statements inside PL/SQL blocks, and you can use procedures supplied by Oracle to perform data definition language (DDL) statements.

PL/SQL code runs on the server, so using PL/SQL lets you centralize significant parts of your database applications for increased maintainability and security. It also enables you to achieve a significant reduction of network overhead in client/server applications.

Note: Some Oracle tools, such as Oracle Forms, contain a PL/SQL engine that lets you run PL/SQL locally.

You can even use PL/SQL for some database applications in place of 3GL programs that use embedded SQL or Oracle Call Interface (OCI).

PL/SQL program units include:

Anonymous Blocks

Stored Program Units (Procedures, Functions, and Packages)

Triggers

See Also:

PL/SQL User's Guide and Reference for syntax and examples of operations on PL/SQL packages

PL/SQL Packages and Types Reference for information about the PL/SQL packages that come with Oracle Database

Anonymous Blocks

An anonymous block is a PL/SQL program unit that has no name and it does not require the explicit presence of the BEGIN and END keywords to enclose the executable statements. An anonymous block consists of an optional declarative part, an executable part, and one or more optional exception handlers.

7-2 Oracle Database Application Developer's Guide - Fundamentals

Overview of PL/SQL Program Units

The declarative part declares PL/SQL variables, exceptions, and cursors. The executable part contains PL/SQL code and SQL statements, and can contain nested blocks. Exception handlers contain code that is called when the exception is raised, either as a predefined PL/SQL exception (such as NO_DATA_FOUND or ZERO_ DIVIDE) or as an exception that you define.

The following short example of a PL/SQL anonymous block prints the names of all employees in department 20 in the Emp_tab table, using the DBMS_OUTPUT package:

DECLARE

 

Emp_name

VARCHAR2(10);

Cursor

c1 IS SELECT Ename FROM Emp_tab

 

WHERE Deptno = 20;

BEGIN

OPEN c1;

LOOP

FETCH c1 INTO Emp_name;

EXIT WHEN c1%NOTFOUND;

DBMS_OUTPUT.PUT_LINE(Emp_name);

END LOOP;

END;

Note: If you test this block using SQL*Plus, then enter the statement SET SERVEROUTPUT ON, so that output using the DBMS_ OUTPUT procedures (for example, PUT_LINE) is activated. Also, end the example with a slash (/) to activate it.

See Also: PL/SQL Packages and Types Reference for complete information about the DBMS_OUTPUT package

Exceptions let you handle Oracle Database error conditions within PL/SQL program logic. This allows your application to prevent the server from issuing an error that could cause the client application to end. The following anonymous block handles the predefined Oracle Database exception NO_DATA_FOUND (which would result in an ORA-01403 error if not handled):

DECLARE

Emp_number INTEGER := 9999;

Emp_name VARCHAR2(10);

BEGIN

SELECT Ename INTO Emp_name FROM Emp_tab

Using Procedures and Packages 7-3

Тут вы можете оставить комментарий к выбранному абзацу или сообщить об ошибке.

Оставленные комментарии видны всем.

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