1.11 Records in PL/SQLA PL/SQL record is a data structure composed of multiple pieces of information called fields . To use a record, you must first define it and declare a variable of this type. There are three types of records: table-based, cursor-based, and programmer-defined. 1.11.1 Declaring RecordsYou define and declare records either in the declaration section of a PL/SQL block, or globally, via a package specification. You do not have to explicitly define table-based or cursor-based records, as they are implicitly defined with the same structure as a table or cursor. Variables of these types are declared via the %ROWTYPE attribute. The record's fields correspond to the table's columns or the columns in the SELECT list. For example:
Programmer-defined records must be explicitly defined in the PL/SQL block or a package specification with the TYPE statement. Variables of this type can then be declared:
1.11.2 Referencing Fields of RecordsIndividual fields are referenced via dot notation: record_name.field_name For example: employee.first_name Individual fields within a record can be read from or written to. They can appear on either the left or right side of the assignment operator:
1.11.3 Record AssignmentAn entire record can be assigned to another record of the same type, but one record cannot be compared to another record via Boolean operators. This is a valid assignment: shipto_address_rec := customer_address_rec This is not a valid comparison: IF shipto_address_rec = customer_address_rec THEN ... END IF; The individual fields of the records need to be compared instead. Values can be assigned to records or to the fields within a record in four different ways:
new_emp_rec.hire_date := SYSDATE;
SELECT emp_id,dept,title,hire_date,college_recruit INTO new_emp_rec FROM emp WHERE surname = 'LI'
FETCH emp_cur INTO new_emp_rec; FETCH emp_cur INTO new_emp_rec.emp_id, new_emp_rec.name;
1.11.4 Nested RecordsNested records are records contained in fields that are records themselves. Nesting records is a powerful way to normalize data structures and hide complexity within PL/SQL programs. For example:
Copyright (c) 2000 O'Reilly & Associates. All rights reserved. |
|