Entering content frame

Reading 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 fgj] | AT LAST.
  ...
  
ENDAT.]

...
ENDLOOP.

When the LOOPstatement occurs, the system stops creating the extract dataset, and starts a loop through all entries contained in it. 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 execute several loops in succession. The loop around an extract dataset 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 arguments of AT as follows:

        AT FIRST

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

        AT fgi [WITH fgj] The system processes the statement block if the record type of the currently read extract record was defined using the field group fgi. When using the WITH fgjaddition, in the extract dataset, the currently rea128d record of field group fgi must be immediately followed by a record of field group fgj.

        AT LAST

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

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

Example

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

REPORT demo_extract_loop.

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 fgi tells the system to output the fields corresponding to each of the two record types. The WITH flight_date addition means that the system only displays the records of field group flight_info if at least one record of field group flight_datefollows; 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 hash 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