Vous êtes sur la page 1sur 7

Defining Record Types Explicitly as PL/SQL Structures

DECLARE
-- Define a record type.
TYPE individual_record IS RECORD
(individual_id INTEGER
,first_name VARCHAR2(30 CHAR)
,middle_initial individuals.middle_initial%TYPE
,last_name VARCHAR2(30 CHAR));
-- Define a variable of the record type.
individual INDIVIDUAL_RECORD;
BEGIN
-- Initialize the field values for the record.
individual.individual_id := 2;
individual.first_name := 'John';
individual.middle_initial := 'P';
individual.last_name := 'Morgan';
-- Insert into the table.
INSERT
INTO individuals
VALUES
(individual.individual_id
,individual.first_name
,individual.middle_initial
,individual.last_name);
-- Commit the work.
COMMIT;
END;

The following example program demonstrates the definition and referencing of


nested record types in a compound record type:
-- Available online as part of create_record3.sql
DECLARE
-- Define a record type.
TYPE individual_record IS RECORD
(individual_id INTEGER
,first_name VARCHAR2(30 CHAR)
,middle_initial VARCHAR2(1 CHAR)
,last_name VARCHAR2(30 CHAR));
-- Define a record type.
TYPE address_record IS RECORD
(address_id INTEGER
,individual_id INTEGER
,street_address1 VARCHAR2(30 CHAR)
,street_address2 VARCHAR2(30 CHAR)
,street_address3 VARCHAR2(30 CHAR)
,city VARCHAR2(20 CHAR)
,state VARCHAR2(20 CHAR)
,postal_code VARCHAR2(20 CHAR)
,country_code VARCHAR2(10 CHAR));
-- Define a record type of two user-defined record types.
TYPE individual_address_record IS RECORD
(individual INDIVIDUAL_RECORD
,address ADDRESS_RECORD);
-- Define a user-defined compound record type.
individual_address INDIVIDUAL_ADDRESS_RECORD;
BEGIN
-- Initialize the field values for the record.
individual_address.individual.individual_id := 3;
individual_address.individual.first_name := 'Ulysses';
individual_address.individual.middle_initial := 'S';
individual_address.individual.last_name := 'Grant';
-- Initialize the field values for the record.
individual_address.address.address_id := 1;
individual_address.address.individual_id := 3;
individual_address.address.street_address1 :=
'Riverside Park';
individual_address.address.street_address2 := ' ';
individual_address.address.street_address3 := '';
individual_address.address.city := 'New York City';
individual_address.address.state := 'New York';
individual_address.address.postal_code := '10027-3914';
individual_address.address.country_code := 'USA';
-- Insert the values into the target object.
INSERT
INTO individuals
VALUES
(individual_address.individual.individual_id
,individual_address.individual.first_name
,individual_address.individual.middle_initial
,individual_address.individual.last_name);
-- Insert the values into the target object.
INSERT
INTO addresses
VALUES
(individual_address.address.address_id
(individual_address.address.individual_id
,individual_address.address.street_address1
,individual_address.address.street_address2
,individual_address.address.street_address2
,individual_address.address.city
,individual_address.address.state
,individual_address.address.postal_code
,individual_address.address.country_code);
-- Commit the record.
COMMIT;
END;
/

The sample program does the following:


■ It defines a record-type variable, individual_record. All fields are
explicitly defined in the PL/SQL program equivalent to the related table.
■ It defines a record-type variable, address_record. All fields are
explicitly defined in the PL/SQL program equivalent to the related table.
■ It defines a record-type variable, individual_address_record. This
new record type is a compound record type. It has two fields, which are
the earlier individual_record and address_record record types.
Each of the record types becomes a subtype of the compound record type.
■ It defines an individual_address variable using the individual_
address_record compound record type as the data type.
■ It initializes the individual_address variable field values for the
individual subtype. Initialization is carried out by assigning values to
the field-level elements in the record type. You initialize by using a twodot
notation. On the left side of the assignment operator, the two-dot
notation separates the variable from subtype record and the subtype record
from the field values as shown here:
RECORD_TYPE.NESTED_RECORD_TYPE.FIELD_TYPE := VALUE;
■ It initializes the field values for the address subtype. Initialization is
accomplished by assigning values to the field-level elements in the
record type.
■ It inserts a row into the individuals table, which is a parent table in the
ERD model. The table is a parent table because of a referential integrity
constraint found in the addresses table.
■ It inserts a row into the addresses table, which is a child table in the
ERD model.
■ It commits the work.
Defining and Using Record
Types as Formal Parameters
DECLARE
-- Define a record type.
TYPE individual_record IS RECORD
(individual_id INTEGER
,first_name VARCHAR2(30 CHAR)
,middle_initial VARCHAR2(1 CHAR)
,last_name VARCHAR2(30 CHAR));
-- Define a record type.
TYPE address_record IS RECORD
(address_id INTEGER
,individual_id INTEGER
,street_address1 VARCHAR2(30 CHAR)
,street_address2 VARCHAR2(30 CHAR)
,street_address3 VARCHAR2(30 CHAR)
,city VARCHAR2(20 CHAR)
,state VARCHAR2(20 CHAR)
,postal_code VARCHAR2(20 CHAR)
,country_code VARCHAR2(10 CHAR));
-- Define a record type of two user-defined record types.
TYPE individual_address_record IS RECORD
(individual INDIVIDUAL_RECORD
,address ADDRESS_RECORD);
-- Define a user-defined compound record type.
individual_address INDIVIDUAL_ADDRESS_RECORD;
-- Define a local procedure to manage addresses inserts.
PROCEDURE insert_address
(address_in ADDRESS_RECORD) IS
BEGIN
-- Insert the values into the target object.
INSERT
INTO addresses
VALUES
(address_in.address_id
,address_in.individual_id
,address_in.street_address1
,address_in.street_address2
,address_in.street_address3
,address_in.city
,address_in.state
,address_in.postal_code
,address_in.country_code);
END insert_address;
-- Define a local procedure to manage addresses inserts.
PROCEDURE insert_individual
(individual_in INDIVIDUAL_RECORD) IS
BEGIN
-- Insert the values into the table.
INSERT
INTO individuals
VALUES
(individual_in.individual_id
,individual_in.first_name
,individual_in.middle_initial
,individual_in.last_name);
END insert_individual;
BEGIN
-- Initialize the field values for the record.
individual_address.individual.individual_id := 6;
individual_address.individual.first_name := 'Ruldolph';
individual_address.individual.middle_initial := '';
individual_address.individual.last_name := 'Gulianni';
-- Initialize the field values for the record.
individual_address.address.address_id := 3;
individual_address.address.individual_id := 6;
individual_address.address.street_address1 := '89th St';
individual_address.address.street_address2 := '';
individual_address.address.street_address3 := '';
individual_address.address.city := 'New York City';
individual_address.address.state := 'NY';
individual_address.address.postal_code := '10028';
individual_address.address.country_code := 'USA';
-- Create a savepoint.
SAVEPOINT addressbook;
-- Process object subtypes.
insert_individual(individual_address.individual);
insert_address(individual_address.address);
 Commit the record.
 COMMIT;
 EXCEPTION
 -- Rollback to savepoint on error.
 WHEN OTHERS THEN
 ROLLBACK to addressbook;
 RETURN;
 END;
 /
 The sample program does the following:
 ■ It defines a record-type variable, individual_record. All fields are
 explicitly defined in the PL/SQL program equivalent to the related table.
 ■ It defines a record-type variable, address_record. All fields are
 explicitly defined in the PL/SQL program equivalent to the related table.
 ■ It defines a record-type variable, individual_address_record. This
 new record type is a compound record type. It has two fields, which are
 the previously defined individual_record and address_record
 record types. Each of the record types becomes a subtype of the
 compound record type.
 ■ It defines an individual_address variable that uses the compound
 record type individual_address_record.
 ■ It defines a local procedure, insert_address. The procedure takes
 a single formal parameter, a variable of the address_record record
 type. The local insert_address procedure inserts a row into the
 addresses table.
 ■ It defines a local procedure, insert_individual. The procedure takes
 a single formal parameter, a variable of the individual_record record
 type. The local insert_individual procedure inserts a row into the
 addresses table.
 ■ It initializes the individual_address variable field values for the
 individual subtype. Initialization is performed by assigning values
 to the field-level elements in the record type.
 ■ It initializes the field values for the address subtype. Initialization is
 performed by assigning values to the field-level elements in the record type.
 ■ It sets a save point for transaction control.
 Chapter 5: Records 201
 ■ It calls the local insert_individual procedure and passes an actual
 parameter of the individual subtype.
 ■ It calls the local insert_address procedure and passes an actual
 parameter of the address subtype.
 ■ It commits the work.

Defining and Using Record Types as Return Values

DECLARE
-- Define a record type.
TYPE individual_record IS RECORD
(individual_id INTEGER
,first_name VARCHAR2(30 CHAR)
,middle_initial individuals.middle_initial%TYPE
,last_name VARCHAR2(30 CHAR));
-- Define a variable of the record type.
individual INDIVIDUAL_RECORD;
-- Define a local function to return a record type.
FUNCTION get_row
(individual_id_in INTEGER)
RETURN INDIVIDUAL_RECORD IS
-- Define a cursor to return a row of individuals.
CURSOR c (individual_id_cursor INTEGER) IS
SELECT *
FROM individuals
WHERE individual_id = individual_id_cursor;
BEGIN
-- Loop through the cursor for a single row.
FOR i IN c(individual_id_in) LOOP
-- Return a %ROWTYPE from the INDIVIDUALS table.
RETURN i;
END LOOP;
END get_row;
BEGIN
-- Demonstrate function return variable assignment.
individual := get_row(1);
-- Display results.
dbms_output.put_line(CHR(10));
dbms_output.put_line('INDIVIDUAL_ID : '
|| individual.individual_id);
dbms_output.put_line('FIRST_NAME : '
|| individual.first_name);
dbms_output.put_line('MIDDLE_INITIAL : '
|| individual.middle_initial);
dbms_output.put_line('LAST_NAME : '
|| individual.last_name);
END;
/

The sample program does the following:


■ It defines a record type, individual_record.
■ It defines a variable individual of the individual_record record type.
■ It defines a local get_row function. The function takes a single formal
parameter of an integer and returns a variable of the individual_
record record type. In the local get_row function, there is a cursor
that takes a single formal parameter and returns a %ROWTYPE from the
individuals table.
■ It uses a cursor for-loop to retrieve a single row from the individuals
table. It then returns the %ROWTYPE selected by the cursor.
■ It assigns the return value from the local get_row function to an individual
variable that uses the same record-type variable.
■ It uses DBMS_OUTPUT utility to print the data.
The program will generate the following output to console:
-- Available as output from online create_function1.sql
INDIVIDUAL_ID : 1
FIRST_NAME : John
MIDDLE_INITIAL : D
LAST_NAME : Rockefeller

Vous aimerez peut-être aussi