You are on page 1of 22

Release Date: August, 2015

Updates:

1
2
3
4
This and the next few slides show that a record declared with %ROWTYPE can be based on a table rather
than on a cursor. Students will probably find this easy to understand, but it is a necessary prerequisite for
the more complex record structures later in the lesson.

5
Point out that this is long-winded and cumbersome code. And what happens if a twelfth column is added to
the EMPLOYEES table? Or an existing column is dropped? Imagine a table with forty or fifty columns (these
are not uncommon in production databases).

6
7
PL/SQL allows any named variable – scalar or composite - to be passed as a parameter.

8
9
An example of a PL/SQL record could be for holding employee information, such as name, salary, hire date,
and so on. A record holds dissimilar, but logically related data that allows the data to be treated as a logical
unit. When you declare a RECORD type containing these fields, they can be manipulated as a unit.

So, if we wanted to work with employee data in a PL/SQL Block, rather than creating 11 local variables in a
PL/SQL block, we can create one single PL/SQL Record to hold all those 11 columns from the table.

10
11
Obviously we cannot declare a record as: p_record_name
bits_of_table1_plus_bits_of_table2_plus….%ROWTYPE ! We could create a database view to implement
the join, and then use %ROWTYPE on the view. But as we have seen, views have limitations, for example
complex views may not be updateable.

Stress that records are not the same as rows in a table, even if (using %ROWTYPE) their structure exactly
matches a table row. Table rows are stored on disk and are permanent; the data in a record is held in a
memory structure (like any other program variable) and persists only for the duration of the session.

Fields not assigned an initial value are initialized to NULL.

The DEFAULT keyword can be used when defining fields

12
type_name is the name of the RECORD type – in essence you are defining a new data type
field_declaration contains the field name(s), data type(s), and any initialization values such as NOT NULL or DEFAULT
identifier creates an instance of the RECORD

If we are creating a record to hold an entire table, then we can skip the TYPE declaration, and simply declare our
record as a table_name%ROWTYPE. This creates a record where the field names are the same as the underlying
column names, and it greatly simplifies the subsequent select statements, as you can always just do a SELECT * INTO
My_PL_SQL_RECORD from table_name;

The Oracle-predefined scalar data types such as VARCHAR2, DATE, NUMBER and so on, are actually TYPEs declared
automatically (with global scope) in every Oracle database. If you have DBA privileges, try the following:

SELECT type_name
FROM dba_types
WHERE predefined = ‘YES’;

13
14
15
The slide example declares two record types and a record based on each of the types.

16
17
Remind students that declarations in a package specification are visible to the calling environment, not just
within the package itself.

18
19
PL/SQL record – is a composite data type consisting of a group of related data items stored as fields, each
with its own name and data type.

20
21
22

You might also like