Entering content frameReading an Extract Locate the document in its SAP Library structure

Like internal tables, you can read the data in an extract dataset using a loop.

LOOP.
...
  [AT FIRST | AT <fgi> [WITH <fg j>] | AT LAST.
  ...
   ENDAT.]
...
ENDLOOP.

When the LOOP statement occurs, the system stops creating the extract dataset, and starts a loop through the entries in the dataset. One record from the extract dataset is read in each loop pass. The values of the extracted fields are placed in the corresponding output fields within the loop. You can use several loops one after the other, but they cannot be nested. It is also no longer possible to use further EXTRACT statements within or after the loop. In both cases, a runtime error occurs.

In contrast to internal tables, extract datasets do not require a special work area or field symbol as an interface. Instead, you can process each record of the dataset within the loop using its original field names.

Loop control

If you want to execute some statements for certain records of the dataset only, use the control statements AT and ENDAT.

The system processes the statement blocks between the control statements for the different options of AT as follows:

The system executes the statement block once for the first record of the dataset.

The system processes the statement block, if the record type of the currently read extract record was defined using the field group <fg i >. When using the WITH <fg j > option, in the extract dataset, the currently read record of field group <fg i > must be immediately followed by a record of field group <fg j >.

The system executes the statement block once for the last record of the dataset.

You can also use the AT and ENDAT statements for control level processing.

Example

Assume the following program is linked to the logical database F1S.

REPORT DEMO.

NODES: SPFLI, SFLIGHT.

FIELD-GROUPS: HEADER, FLIGHT_INFO, FLIGHT_DATE.

INSERT: SPFLI-CARRID SPFLI-CONNID SFLIGHT-FLDATE
INTO HEADER,
SPFLI-CITYFROM SPFLI-CITYTO
INTO FLIGHT_INFO.

START-OF-SELECTION.

GET SPFLI.
  EXTRACT FLIGHT_INFO.

GET SFLIGHT.
  EXTRACT FLIGHT_DATE.

END-OF-SELECTION.

  LOOP.
    AT FIRST.
      WRITE / 'Start of LOOP'.
      ULINE.
    ENDAT.
    AT FLIGHT_INFO WITH FLIGHT_DATE.
      WRITE: / 'Info:',
               SPFLI-CARRID, SPFLI-CONNID, SFLIGHT-FLDATE,
               SPFLI-CITYFROM, SPFLI-CITYTO.
    ENDAT.
    AT FLIGHT_DATE.
      WRITE: / 'Date:',
                SPFLI-CARRID, SPFLI-CONNID, SFLIGHT-FLDATE.
    ENDAT.
    AT LAST.
ULINE.
      WRITE / 'End of LOOP'.
    ENDAT.
  ENDLOOP.

The extract dataset is created and filled in the same way as shown in the example for Filling an Extract with Data. The data retrieval ends before the END-OF-SELECTION event, in which the dataset is read once using a loop.

The control statements AT FIRST and AT LAST instruct the system to write one line and one underscore line in the list, once at the beginning of the loop and once at the end.

The control statement AT <fg i > tells the system to output the fields corresponding to each of the two record types. The WITH FLIGHT_DATE option means that the system only displays the records of field group FLIGHT_INFO if at least one record of field group FLIGHT_DATE follows; that is, if the logical database passed at least one date for a flight.

The beginning of the output list looks like this:

This graphic is explained in the accompanying text

The contents of the field SFLIGHT-FLDATE in the HEADER part of record type FLIGHT_INFO are displayed as pound signs (#). This is because the logical database fills all of the fields at that hierarchy level with the value HEX 00 when it finishes processing that level. This feature is important for sorting and for processing control levels in extract datasets.

 

 

 

 

Leaving content frame