Refining Data Using Internal Tables 

When storing data in internal tables, you often use one internal table for each database you read. Each one contains some or all columns of the relevant database table. It is up to you whether you create an internal table with a flat structure for each database table or if you create, for example, internal tables with nested structures. If you have several tables, each one with a flat structure, you have to work with redundant key fields to link the tables. If, on the other hand, you use nested internal tables, you can store the data from the database tables hierarchically.

Saving and processing very large amounts of data in internal tables has disadvantages. If you divide up the data into different internal tables, processing it can be very runtime-intensive, since the tables have to be processed individually. Furthermore, it requires a lot of storage space, since internal tables are not stored in compressed form. The system may even need to store the dataset outside of its working memory. This means that processing it takes even longer.

Processing Data Using Flat Internal Tables

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

REPORT DEMO.

DATA: SUM TYPE I, CNT TYPE I.

NODES: SPFLI, SFLIGHT, SBOOK.

DATA: TAB_SPFLI   TYPE TABLE OF SPFLI,
      TAB_SFLIGHT TYPE TABLE OF SFLIGHT,
      TAB_SBOOK   TYPE TABLE OF SBOOK.

DATA: WA_SPFLI   LIKE LINE OF TAB_SPFLI,
      WA_SFLIGHT LIKE LINE OF TAB_SFLIGHT,
      WA_SBOOK   LIKE LINE OF TAB_SBOOK.

START-OF-SELECTION.

GET SPFLI.
  APPEND SPFLI TO TAB_SPFLI.

GET SFLIGHT.
  APPEND SFLIGHT TO TAB_SFLIGHT.

GET SBOOK.
  APPEND SBOOK TO TAB_SBOOK.

END-OF-SELECTION.

  SORT: TAB_SPFLI   BY CITYFROM CITYTO CONNID,
        TAB_SFLIGHT BY FLDATE,
        TAB_SBOOK   BY CLASS SMOKER BOOKID.

  LOOP AT TAB_SPFLI INTO WA_SPFLI.

    SKIP.
    WRITE: / WA_SPFLI-CARRID,
             WA_SPFLI-CONNID,
             'from', (15) WA_SPFLI-CITYFROM,
             'to',   (15) WA_SPFLI-CITYTO.
    ULINE.

    LOOP AT TAB_SFLIGHT INTO WA_SFLIGHT
                        WHERE CARRID = WA_SPFLI-CARRID
                          AND CONNID = WA_SPFLI-CONNID.

      SKIP.
      WRITE: / 'Date:', WA_SFLIGHT-FLDATE.
      WRITE: 20 'Book-ID', 40 'Smoker', 50 'Class'.
      ULINE.

      SUM = 0.
      CNT = 0.
      LOOP AT TAB_SBOOK INTO WA_SBOOK
                        WHERE CARRID = WA_SFLIGHT-CARRID
                          AND CONNID = WA_SFLIGHT-CONNID
                          AND FLDATE = WA_SFLIGHT-FLDATE.

        WRITE: / WA_SBOOK-BOOKID UNDER 'Book-ID',
                 WA_SBOOK-SMOKER UNDER 'Smoker',
                 WA_SBOOK-CLASS  UNDER 'Class'.

                 SUM = SUM + WA_SBOOK-LUGGWEIGHT.
                 CNT = CNT + 1.

      ENDLOOP.

      ULINE.
      WRITE: 'Number of bookings:  ', (3) CNT,
           / 'Total luggage weight:',
          (3) SUM, WA_SBOOK-WUNIT.

    ENDLOOP.

  ENDLOOP.

This program creates the same list as in the Example of Formatted Data.

The GET events that retrieve the data are clearly separated from the sorting process. The three internal tables have exactly the same structure and contents as the corresponding database tables. The data is sorted and then displayed. The loop structure is exactly the same as that of the SELECT loops in the example from the Formatting Data During Reading section.

Formatting Data Using Nested Internal Tables

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

REPORT DEMO.

DATA: SUM TYPE I, CNT TYPE I.

NODES: SPFLI, SFLIGHT, SBOOK.

DATA: BEGIN OF WA_SBOOK,
        BOOKID     TYPE SBOOK-BOOKID,
        SMOKER     TYPE SBOOK-SMOKER,
        CLASS      TYPE SBOOK-CLASS,
        LUGGWEIGHT TYPE SBOOK-LUGGWEIGHT,
        WUNIT      TYPE SBOOK-WUNIT,
      END OF WA_SBOOK.

DATA: BEGIN OF WA_SFLIGHT,
        FLDATE TYPE SFLIGHT-FLDATE,
        SBOOK  LIKE
TABLE OF WA_SBOOK ,
      END OF WA_SFLIGHT.

DATA: BEGIN OF WA_SPFLI,
        CARRID   TYPE SPFLI-CARRID,
        CONNID   TYPE SPFLI-CONNID,
        CITYFROM TYPE SPFLI-CITYFROM,
        CITYTO   TYPE SPFLI-CITYTO,
        SFLIGHT  LIKE
TABLE OF WA_SFLIGHT ,
      END OF WA_SPFLI.

DATA TAB_SPFLI LIKE TABLE OF WA_SPFLI .

START-OF-SELECTION.

GET SPFLI.
  REFRESH WA_SPFLI-SFLIGHT.

GET SFLIGHT.
  REFRESH WA_SFLIGHT-SBOOK.

GET SBOOK.
  MOVE-CORRESPONDING SBOOK TO WA_SBOOK.
  APPEND WA_SBOOK TO WA_SFLIGHT-SBOOK.

GET SFLIGHT LATE.
  MOVE-CORRESPONDING SFLIGHT TO WA_SFLIGHT.
  APPEND WA_SFLIGHT TO WA_SPFLI-SFLIGHT.

GET SPFLI LATE.
  MOVE-CORRESPONDING SPFLI TO WA_SPFLI.
  APPEND WA_SPFLI TO TAB_SPFLI.

END-OF-SELECTION.

  SORT TAB_SPFLI BY CITYFROM CITYTO CONNID.

  LOOP AT TAB_SPFLI INTO WA_SPFLI.

    SKIP.
    WRITE: / WA_SPFLI-CARRID,
             WA_SPFLI-CONNID,
             'from', (15) WA_SPFLI-CITYFROM,
             'to',   (15) WA_SPFLI-CITYTO.
    ULINE.

    SORT WA_SPFLI-SFLIGHT BY FLDATE.

    LOOP AT WA_SPFLI-SFLIGHT INTO WA_SFLIGHT.

      SKIP.
      WRITE: / 'Date:', WA_SFLIGHT-FLDATE.
      WRITE: 20 'Book-ID', 40 'Smoker', 50 'Class'.
      ULINE.

      SORT WA_SFLIGHT-SBOOK BY CLASS SMOKER BOOKID.

      SUM = 0.
      CNT = 0.

      LOOP AT WA_SFLIGHT-SBOOK INTO WA_SBOOK.

        WRITE: / WA_SBOOK-BOOKID UNDER 'Book-ID',
                 WA_SBOOK-SMOKER UNDER 'Smoker',
                 WA_SBOOK-CLASS  UNDER 'Class'.

        SUM = SUM + WA_SBOOK-LUGGWEIGHT.
        CNT = CNT + 1.

      ENDLOOP.

      ULINE.
      WRITE: 'Number of bookings:  ', (3) CNT,
           / 'Total luggage weight:',
          (3) SUM, WA_SBOOK-WUNIT.

    ENDLOOP.

  ENDLOOP.

This program creates the same list as in the Example of Formatted Data.

During the GET events, the system reads the data into the three-level internal table SORT_SPFLI which contains the substructure SFLIGHT and its substructure SBOOK. The sorting process takes place on the individual nesting levels.

This way of programming does not require key relations between the internal tables (no WHERE conditions), but it is more complex than using flat internal tables. And the increased internal administration effort has a negative effect on the storage space required as well as on the runtime.