DECLARE
  -- Declare a record type with 3 fields.
  TYPE REC1_T IS RECORD(FIELD1 VARCHAR2(16),
                         FIELD2 NUMBER,
                         FIELD3 DATE);
   -- For any fields declared NOT NULL, we must supply a default value.
   TYPE REC2_T IS RECORD(ID INTEGER NOT NULL := -1,
                          NAME VARCHAR2(64) NOT NULL := '[anonymous]');
   -- Declare record variables of the types declared
   REC1  REC1_T;
   REC2  REC2_T;
   -- Declare a record variable that can hold a row from the EMPLOYEES table.
   -- The fields of the record automatically match the names and
   -- types of the columns.
   -- Don't need a TYPE declaration in this case.
   REC3  EMPLOYEES%ROWTYPE;
   -- Or we can mix fields that are table columns with user-defined fields.
   TYPE REC4_T IS RECORD(FIRST_NAME EMPLOYEES.FIRST_NAME%TYPE,
                          LAST_NAME EMPLOYEES.LAST_NAME%TYPE,
                          RATING NUMBER);
   REC4  REC4_T;
BEGIN
  -- Read and write fields using dot notation
  REC1.FIELD1 := 'Yesterday';
 
  REC1.FIELD2 := 65;
 
  REC1.FIELD3 := TRUNC(SYSDATE - 1);
 
  -- We didn't fill in the name field, so it takes the default value declared
  DBMS_OUTPUT.PUT_LINE(REC2.NAME);
END;
/

Declaring and Initializing Record Types
0 comments:
Post a Comment