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

Understanding the Main Features of PL/SQL

Writing Reusable PL/SQL Code

PL/SQL lets you break an application down into manageable, well-defined modules. PL/SQL meets this need with program units, which include blocks, subprograms, and packages. You can reuse program units by loading them into the database as triggers, stored procedures, and stored functions. For additional information, see Chapter 8, "Using PL/SQL Subprograms" and Chapter 9, "Using PL/SQL Packages".

Subprograms: Procedures and Functions

There are two types of subprograms called procedures and functions, which can accept parameters and be invoked (called). See "What Are Subprograms?" on page 8-1.

The SQL CREATE PROCEDURE statement lets you create standalone procedures that are stored in the database. For information, see CREATE PROCEDURE in Oracle Database SQL Reference. The SQL CREATE FUNCTION statement lets you create standalone functions that are stored in an Oracle database. For information, see CREATE FUNCTION in Oracle Database SQL Reference. These stored (schema level) subprograms can be accessed from SQL.

As shown in Example 1–12, a subprogram is like a miniature program, beginning with a header followed by an optional declarative part, an executable part, and an optional exception-handling part.

Example 1–12 Creating a Stored Subprogram

-- including OR REPLACE is more convenient when updating a subprogram CREATE OR REPLACE PROCEDURE award_bonus (emp_id NUMBER, bonus NUMBER) AS

commission REAL; comm_missing EXCEPTION;

BEGIN -- executable part starts here

SELECT commission_pct / 100 INTO commission FROM employees WHERE employee_id = emp_id;

IF commission IS NULL THEN RAISE comm_missing;

ELSE

UPDATE employees SET salary = salary + bonus*commission WHERE employee_id = emp_id;

END IF;

EXCEPTION -- exception-handling part starts here WHEN comm_missing THEN

DBMS_OUTPUT.PUT_LINE('This employee does not receive a commission.'); commission := 0;

WHEN OTHERS THEN

NULL; -- for other exceptions do nothing END award_bonus;

/

CALL award_bonus(150, 400);

When called, this procedure accepts an employee Id and a bonus amount. It uses the Id to select the employee's commission percentage from a database table and, at the same time, convert the commission percentage to a decimal amount. Then, it checks the commission amount. If the commission is null, an exception is raised; otherwise, the employee's salary is updated.

Packages: APIs Written in PL/SQL

PL/SQL lets you bundle logically related types, variables, cursors, and subprograms into a package, a database object that is a step above regular stored procedures. The

Overview of PL/SQL 1-13

Understanding the Main Features of PL/SQL

packages defines a simple, clear, interface to a set of related procedures and types that can be accessed by SQL statements.

Packages usually have two parts: a specification and a body. The specification defines the application programming interface; it declares the types, constants, variables, exceptions, cursors, and subprograms. The body fills in the SQL queries for cursors and the code for subprograms.

To create package specs, use the SQL statement CREATE PACKAGE. A CREATE PACKAGE BODY statement defines the package body. For information on the CREATE PACKAGE SQL statement, see Oracle Database SQL Reference. For information on the

CREATE PACKAGE BODY SQL statement, see Oracle Database SQL Reference.

In Example 1–13, the emp_actions package contain two procedures that update the employees table and one function that provides information.

Example 1–13 Creating a Package and Package Body

CREATE OR REPLACE PACKAGE emp_actions AS -- package specification PROCEDURE hire_employee (employee_id NUMBER, last_name VARCHAR2,

first_name VARCHAR2, email VARCHAR2, phone_number VARCHAR2, hire_date DATE, job_id VARCHAR2, salary NUMBER, commission_pct NUMBER, manager_id NUMBER, department_id NUMBER);

PROCEDURE fire_employee (emp_id NUMBER);

FUNCTION num_above_salary (emp_id NUMBER) RETURN NUMBER; END emp_actions;

/

CREATE OR REPLACE PACKAGE BODY emp_actions AS -- package body

--code for procedure hire_employee

PROCEDURE hire_employee (employee_id NUMBER, last_name VARCHAR2,

first_name VARCHAR2, email VARCHAR2, phone_number VARCHAR2, hire_date DATE, job_id VARCHAR2, salary NUMBER, commission_pct NUMBER,

manager_id NUMBER, department_id NUMBER) IS BEGIN

INSERT INTO employees VALUES (employee_id, last_name, first_name, email, phone_number, hire_date, job_id, salary, commission_pct, manager_id, department_id);

END hire_employee;

--code for procedure fire_employee PROCEDURE fire_employee (emp_id NUMBER) IS BEGIN

DELETE FROM employees WHERE employee_id = emp_id; END fire_employee;

--code for function num_above salary

FUNCTION num_above_salary (emp_id NUMBER) RETURN NUMBER IS emp_sal NUMBER(8,2);

num_count NUMBER; BEGIN

SELECT salary INTO emp_sal FROM employees WHERE employee_id = emp_id; SELECT COUNT(*) INTO num_count FROM employees WHERE salary > emp_sal; RETURN num_count;

END num_above_salary; END emp_actions;

/

Applications that call these procedures only need to know the names and parameters from the package specification. You can change the implementation details inside the package body without affecting the calling applications.

To call the procedures of the emp_actions package created in Example 1–13, you can execute the statements in Example 1–14. The procedures can be executed in a BEGIN ..

1-14 Oracle Database PL/SQL User’s Guide and Reference

Соседние файлы в папке PL_SQL