Professional Documents
Culture Documents
– The set of rows returned by a multi-row query is called the result set. An explicit
cursor "points" to the current row in the result set. This allows your program to
process the rows one at a time.
PL/SQL Cursors
• Manipulating Cursors
– You use the OPEN, FETCH, and CLOSE statements to control a cursor. The OPEN statement executes the
query associated with the cursor, identifies the result set, and positions the cursor before the first row. The
FETCH statement retrieves the current row and advances the cursor to the next row. When the last row has
been processed, the CLOSE statement disables the cursor.
• Cursor FOR Loops
– In most situations that require an explicit cursor, you can simplify coding by using a cursor FOR loop instead
of the OPEN, FETCH, and CLOSE statements.
– A cursor FOR loop implicitly declares its loop index as a record that represents a row in a database table,
opens a cursor, repeatedly fetches rows of values from the result set into fields in the record, then closes the
cursor when all rows have been processed. In the following example, the cursor FOR loop implicitly declares
emp_rec as a record:
• DECLARE
• CURSOR c1 IS
• SELECT ename, sal, hiredate, deptno FROM emp;
• ...
• BEGIN
• FOR emp_rec IN c1 LOOP
• ...
• salary_total := salary_total + emp_rec.sal;
• END LOOP;
• END;
– You use dot notation to reference individual fields in the record.
PL/SQL Cursors
• Cursor Variables
– Like a cursor, a cursor variable points to the current row in the result set of a multi-row query. But,
unlike a cursor, a cursor variable can be opened for any type-compatible query. It is not tied to a
specific query. Cursor variables are true PL/SQL variables, to which you can assign new values and
which you can pass to subprograms stored in an Oracle database. This gives you more flexibility and
a convenient way to centralize data retrieval.
– Typically, you open a cursor variable by passing it to a stored procedure that declares a cursor
variable as one of its formal parameters. The following packaged procedure opens the cursor
variable generic_cv for the chosen query:
• CREATE PACKAGE BODY emp_data AS
• PROCEDURE open_cv (generic_cv IN OUT GenericCurTyp,
• choice IN NUMBER) IS
• BEGIN
• IF choice = 1 THEN
• OPEN generic_cv FOR SELECT * FROM emp;
• ELSIF choice = 2 THEN
• OPEN generic_cv FOR SELECT * FROM dept;
• ELSIF choice = 3 THEN
• OPEN generic_cv FOR SELECT * FROM salgrade;
• END IF;
• END open_cv;
• END emp_data;
Attributes
• Attributes
– PL/SQL variables and cursors have attributes, which are properties that let you reference the datatype and
structure of an object without repeating its definition. Database columns and tables have similar attributes,
which you can use to ease maintenance.
• %TYPE
– The %TYPE attribute provides the datatype of a variable or database column. Example:
• my_title books.title%TYPE;
– Declaring my_title with %TYPE has two advantages. First, you need not know the exact datatype of title.
Second, if you change the database definition of title, the datatype of my_title changes accordingly at run time.
• %ROWTYPE
– In PL/SQL, records are used to group data. The %ROWTYPE attribute provides a record type that represents a
row in a table. The record can store an entire row of data selected from the table or fetched from a cursor or
cursor variable.
– Columns in a row and corresponding fields in a record have the same names and datatypes.
Example:
• DECLARE
• dept_rec dept%ROWTYPE; -- declare record var.
– You use dot notation to reference fields.
Example:
• my_deptno := dept_rec.deptno;
Attributes
– If you declare a cursor that retrieves the last name, salary, hire date,
and job title of an employee, you can use %ROWTYPE to declare a
record that stores the same information, as follows:
• DECLARE
• CURSOR c1 IS
• SELECT ename, sal, hiredate, job FROM emp;
• emp_rec c1%ROWTYPE; -- record variable that
• -- represents a row in the emp table
– When you execute the statement
• FETCH c1 INTO emp_rec;
– the value in the ename column of the emp table is assigned to the
ename field of emp_rec, the value in the sal column is assigned to the
sal field, and so on.
PL/SQL Tables and Record Data Types
PL/SQL Tables
Like an array, a PL/SQL table is an ordered collection of elements of the same type. Each element has
a unique index number that determines its position in the ordered collection.
PL/SQL tables are unbounded (unlike an array) and can be allocated dynamically.
PL/SQL tables do not require consecutive indexnumbers.
You can use a cursor FOR loop to fetch an entire column or table of Oracle data into a PL/SQL table. In
the following example, you fetch a table of data into the PL/SQL table dept_tab:
DECLARE
TYPE DeptTabTyp IS TABLE OF dept%ROWTYPE
INDEX BY BINARY_INTEGER;
dept_tab DeptTabTyp;
n BINARY_INTEGER := 0;
BEGIN
FOR dept_rec IN (SELECT * FROM dept) LOOP
n := n + 1;
dept_tab(n) := dept_rec;
END LOOP;
...
END;
PL/SQL Tables and Record Data Types
User-Defined Records
You can use the %ROWTYPE attribute to declare a record that represents a row in a table or a
row fetched from a cursor. But, with a user-defined record, you can declare fields of your own.
Records contain uniquely named fields, which can have different datatypes. Suppose you have
various data about an employee such as name, salary, and hire date. These items are
dissimilar in type but logically related. A record containing a field for each item lets you treat
the data as a logical unit.
Example:
DECLARE
TYPE TimeTyp IS RECORD (minute SMALLINT, hour SMALLINT);
TYPE MeetingTyp IS RECORD (
day DATE,
time TimeTyp, -- nested record
place VARCHAR2(20),
purpose VARCHAR2(50));
Notice that you can nest records. That is, a record can be the component of another record.
Procedures and Packages
• Modularity
– Modularity lets you break an application down into manageable, well-defined logic modules.
Besides blocks and subprograms, PL/SQL provides the package, which allows you to group related
program objects into larger units.
• Subprograms
– PL/SQL has two types of subprograms called procedures and functions, which can take parameters
and be invoked (called). 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.
• Packages
– PL/SQL lets you bundle logically related types, program objects, and subprograms into a package.
Each package is easy to understand and the interfaces between packages are simple, clear, and
well defined. This aids application development.
– Packages usually have two parts: a specification and a body. The specification is the interface to
your applications; it declares the types, constants, variables, exceptions, cursors, and subprograms
available for use. The body defines cursors and subprograms and so implements the specification.
– Only the declarations in the package specification are visible and accessible to applications.
Implementation details in the package body are hidden and inaccessible.
– Packages can be compiled and stored in an Oracle database, .... When you call a packaged
subprogram for the first time, the whole package is loaded into memory.
Error Handling
Error Handling
PL/SQL makes it easy to detect and process predefined and
user-defined error conditions called exceptions.
When an error occurs, an exception is raised. Normal execution
stops and control transfers to the exception-handling part of
your PL/SQL block or subprogram. To handle raised
exceptions, you write separate routines called exception
handlers.
Predefined exceptions are raised implicitly by the runtime
system.
You must raise user-defined exceptions explicitly with the RAISE
statement.
Error Handling
You can define exceptions of your own in the declarative part of any PL/SQL block or subprogram. In
the executable part, you check for the condition that needs special attention. If you find that the
condition exists, you execute a RAISE statement. Example:
DECLARE
salary NUMBER(7,2);
commission NUMBER(7,2);
comm_missing EXCEPTION; -- declare exception
BEGIN
SELECT sal, comm INTO salary, commission FROM emp
WHERE empno = :emp_id;
IF commission IS NULL THEN
RAISE comm_missing; -- raise exception
ELSE
:bonus := (salary * 0.05) + (commission * 0.15);
END IF;
EXCEPTION -- begin exception handlers
WHEN comm_missing THEN
-- process error
END;