5.4    Processing Data from Database via Internal Tables and Structures

Internal tables and structures are primarily used to process data from database tables. Because all transaction data is stored in database tables, we use internal tables and structures in almost all ABAP programs. Working with internal tables may seem a bit tricky initially, especially when working with multiple tables.

When processing data from multiple internal tables, you should always avoid nested loops (a loop within a loop) because they degrade performance. We always loop the main table and read the secondary tables.

When you have a requirement to process data from more than one table, you may struggle to identify which internal table to loop and which one to read. For example, say you have a requirement to process material and plant data, and this data is available in database table MARC. To print the plant details of a material in the output, write the code shown in Listing 5.31.

TYPES: BEGIN OF ty_marc,
matnr TYPE matnr,
werks TYPE werks_d,
END OF ty_marc.
DATA : it_marc TYPE STANDARD TABLE OF ty_marc,
wa_marc TYPE ty_marc.
PARAMETERS p_matnr TYPE matnr.
SELECT matnr werks from marc INTO TABLE it_marc WHERE matnr EQ p_matnr.
LOOP AT it_marc INTO wa_marc.
WRITE: / wa_marc-matnr, wa_marc-werks.
ENDLOOP.

Listing 5.31    Sample Code to Print Material Plant Data

Now, if the code in Listing 5.31 has to be expanded to print the plant description as the third column for every plant number, you need to select the plant description from another table, T001W, and compare against the plant data in table MARC, as shown in Listing 5.32.

TYPES: BEGIN OF ty_marc,
matnr TYPE matnr,
werks TYPE werks_d,
END OF ty_marc.
TYPES : BEGIN OF ty_t001w,
Werks TYPE werks_d,
Name1 TYPE name1,
END OF ty_t001w.
DATA : it_marc TYPE STANDARD TABLE OF ty_marc,
wa_marc TYPE ty_marc,
it_t001w TYPE STANDARD TABLE OF ty_t001w,
wa_t001w TYPE ty_t001w.
PARAMETERS p_matnr TYPE matnr.
SELECT matnr werks from marc INTO TABLE it_marc WHERE matnr EQ p_matnr.
IF sy-subrc IS INITIAL.
SELECT werks name1 FROM t001w INTO TABLE it_t001w FOR ALL ENTRIES IN it_marc WHERE werks EQ it_marc-werks.
ENDIF.
LOOP AT it_marc INTO wa_marc.
READ TABLE it_t001w INTO wa_t001w WITH KEY werks = wa_marc-werks.
WRITE: / wa_marc-matnr, wa_marc-werks, wa_t001w-name1.
ENDLOOP.

Listing 5.32    Printing Plant Descriptions

As you can see in Listing 5.32, we selected the required information from two database tables into two internal tables by matching the entries of the first table with the second table. After selecting the data into the respective internal tables, we loop one table and read the other table in the loop to print the output.

Now, how do you decide which table to loop, and which table to read? This is where many beginners become confused, but the answer is simple: Loop the table that has the main data, and read the table that has the secondary data. In this example, the main data we’re printing is the plant details of the material; the plant descriptions are just secondary information. Therefore, we’re looping the main table MARA and reading the secondary table T001W.