Declaring a record that redefines another

You may wish to declare a record that redefines an area of memory declared by another record. For example, you can write a loop that reads one data area after another from a serial file, when the structure of the retrieved data is different from one retrieval to the next, as in the following example:
Record RecordA type SerialRecord
   { fileName = "myFile"  }
   record_type char(1);
   item1 char(20); 
end 

Record RecordB type BasicRecord
   10 record_type char(1);
   10 item2 bigint;
   10 item3 decimal(7);
   10 item4 char(8); 
end

Program ProgramX type basicProgram
   myRecordA RecordA;
   myRecordB RecordB {redefines = "myRecordA"};
   
   function main();
     get myRecordA;
     while (myRecordA not endOfFile)
       if (myRecordA.record_type == "A")
         myFunction01(myRecordA.item1);
       else
         myFunction02(myRecordB.item2, myRecordB.item3, myRecordB.item4);
       end
       get myRecordA;
     end
   end 
end
Within the loop, the function acts as follows:
  1. Checks the first field in the input record for a code that identifies how the rest of the data is structured.
  2. Processes the other fields in the retrieved data by using either the input record or a second, basic record. The second record refers to the same area of memory as the input record but is structured differently.

To declare one record as a redefinition of another, you use the property redefines, which accepts a string that identifies another record. This property is only available in a record declaration, not in a record part definition.

The original and overlay record can be any types of fixed record, with the following restrictions:

An overlay record does not have any of the information (other than structure) that is in the original record. An indexed record can redefine a serial record, for example, but the file accessed by the indexed record is identified in the indexed record part and not in the serial record part.

Related concepts
References to variables in EGL

Related concepts
Declaring variables and constants in EGL

Related reference
SQL record internals

Feedback
(C) Copyright IBM Corporation 2000, 2005. All Rights Reserved.