Assume the following program is linked to the logical database [Page 1210] 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,
Refining Data Using Internal Tables
December 1999 357
'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 [Page 352].
The GET [Page 999] 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 [Page 354] section.
Formatting Data Using Nested Internal Tables
Assume the following program is linked to the logical database [Page 1210] 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,
Refining Data Using Internal Tables
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.
Refining Data Using Internal Tables
December 1999 359
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 [Page 352].
During the GET [Page 999] 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.
Formatting Data Using Extracts
Formatting Data Using Extracts
You can use a single extract to store entries from more than one database table, since the records in an extract can have different structures.
Assume the following program is linked to the logical database [Page 1210] F1S.
REPORT DEMO.
NODES: SPFLI, SFLIGHT, SBOOK.
FIELD-GROUPS: HEADER, FLIGHT_INFO, FLIGHT_BOOKING.
INSERT:
SPFLI-CITYFROM SPFLI-CITYTO SPFLI-CONNID SFLIGHT-FLDATE
SBOOK-CLASS SBOOK-SMOKER SBOOK-BOOKID INTO HEADER, SPFLI-CARRID INTO FLIGHT_INFO,
SBOOK-LUGGWEIGHT SBOOK-WUNIT INTO FLIGHT_BOOKING.
START-OF-SELECTION.
GET SPFLI.
EXTRACT FLIGHT_INFO.
GET SFLIGHT.
GET SBOOK.
EXTRACT FLIGHT_BOOKING.
END-OF-SELECTION.
SORT.
LOOP.
AT FLIGHT_INFO.
SKIP.
WRITE: / SPFLI-CARRID, SPFLI-CONNID,
'from', (15) SPFLI-CITYFROM, 'to', (15) SPFLI-CITYTO.
ULINE.
ENDAT.
AT NEW SFLIGHT-FLDATE.
SKIP.
WRITE: / 'Date:', SFLIGHT-FLDATE.
WRITE: 20 'Book-ID', 40 'Smoker', 50 'Class'.
ULINE.
ENDAT.
AT FLIGHT_BOOKING.
WRITE: / SBOOK-BOOKID UNDER 'Book-ID', SBOOK-SMOKER UNDER 'Smoker', SBOOK-CLASS UNDER 'Class'.
ENDAT.
AT END OF SFLIGHT-FLDATE.
Formatting Data Using Extracts
December 1999 361
ULINE.
WRITE: 'Number of bookings: ', (3) CNT(SBOOK-BOOKID), / 'Total luggage weight:',
SUM(SBOOK-LUGGWEIGHT), SBOOK-WUNIT.
ENDAT.
ENDLOOP.
This program creates the same list as in the Example of Formatted Data [Page 352].
The system creates three field groups and fills them with several fields. It then fills the extract in the EXTRACT statements in the GET [Page 999] events. Note that there is no EXTRACT statement for GET SFLIGHT, since the required field SFLIGHT_FLDATE is part of the HEADER field group and thus automatically extracted for each subordinate event GET SBOOK.
After retrieving the data, the system terminates the creation of the dataset when the SORT statement occurs, and sorts the dataset by the HEADER sort key. In the LOOP-ENDLOOP block, it writes the sorted extract dataset to the output list, using various AT ... ENDAT blocks and the fields CNT(...) and SUM(...).