Show TOC Entering content frame

Processing Table Entries in Loops Locate the document in its SAP Library structure

You can use the LOOP statement to process special loops for any internal table.

LOOP AT itab result condition.
  statement block
ENDLOOP.

This reads the lines of the table one by one as specified in the result part of the LOOP statement. You can then process them in the statements within the LOOP - ENDLOOP control structure. You can either run the loop for all entries in the internal table, or restrict the number of lines read by specifying a condition. Control level processing is allowed within the loop.

The sequence in which the lines are processed depends on the table type:

·        Standard tables and sorted tables

The lines are processed according to the linear index. Within the processing block, the system field sy-tabix contains the index of the current line.

·        Hashed tables

As long as the table has not been sorted, the lines are processed in the order in which you added them to the table. Within the processing block, the system field sy-tabix is always 0.

You can nest LOOP blocks. When you leave the loop, sy-tabix has the same value as when you entered it. After the ENDLOOP statement, sy-subrc is zero if at least one table entry was processed. Otherwise, it is 4.

The loop may not contain any operations on entire internal tables  that change the table. However, you should remember that even saving a global internal table with the LOCAL statement in a procedure is a change operation on the entire table, since it exchanges the table contents. When you call procedures within loops, you should therefore check that it does not change the entire internal table. If you change the table, the loop can no longer work properly.

If you insert or delete a table entry within a loop pass, it is taken into account in subsequent loop passes as follows:

·        If you insert a line after the current line, it will be processed in a subsequent loop pass.

·        If you delete a line after the current line, it will not be processed in a subsequent loop pass.

·        If you insert a line before or at the current line, the internal loop counter will be increased accordingly.

·        If you delete a line before or at the current line, the internal loop counter will be decreased accordingly.

Specifying the Extra Processing Option

The processing option specifies how a table line is available in the statement block of the list.

Using a Work Area

To place the current loop line into a work area, specify result as follows:

LOOP AT itab INTO wa bedingung.

The contents of the table lines must be convertible into the data type of the work area wa. In each loop pass, one line of the table is copied into the work area. The end of the loop does not affect the work area, that is, the contents of wa are the same after the ENDLOOP statement as they were in the final loop pass. If no table entries are processed in the loop, because the table is entry, or no line meets the condition cond, the work area is not changed.

Using a Field Symbol

To assign the contents of the current loop line to a field symbol, specify result as follows:

LOOP AT itab ASSIGNING <fs> condition.

In each loop pass, the field symbol <fs> points to the table entry read in that pass. If the line type is structured, you should specify the same type for the field symbol when you declare  it. This allows you to address the components of the field symbol. If you cannot specify the type statically, you must use further field symbols and the technique of assigning components of structures to address the components of the structure.

The end of the loop does not affect the field symbol, that is, after ENDLOOP it is still assigned to the same line as in the final loop pass. If no table entries are processed in the loop, because the table is entry, or no line meets the condition cond, the field symbol is not changed.

For further information about assigning table lines to field symbols, refer to Access Using Field Symbols.

Suppressing the Assignment of Lines

If you do not need to transfer the contents of the current table line to a work area or assign them to a field symbol, you can use the following statement:

LOOP AT itab TRANSPORTING NO FIELDS condition.

This form of the LOOP statement is useful if you want to find the index of a particular internal table in the system field sy-tabix, or the number of rows in a table that meet a particular condition.

Enter conditions

To avoid reading all of the lines in an internal table, you can specify a condition for the line selection as follows:

LOOP AT itab result WHERE cond.

This processes all of the lines that meet the logical condition  cond. The logical expression cond can consist of more than one comparison. In each comparison, the first operand must be a component of the line structure. If the table lines are not structured, the first operand can also be the expression table_line. The comparison then applies to the entire line.

If the line type of the internal table contains object reference variables as component comp or if the entire line type is a reference variable, the attributes of the attr object to which the respective line reference points, can be specified as first field of a comparison using comp->attr or table_line->attr.

Control level processing

Control level processing is allowed within a LOOP over an internal table. This means that you can divide sequences of entries into groups based on the contents of certain fields.

Internal tables are divided into groups according to the sequence of the fields in the line structure. The first column defines the highest control level and so on. The control level hierarchy must be known when you create the internal table.

The control levels are formed by sorting the internal table in the sequence of its structure, that is, by the first field first, then by the second field, and so on. Tables in which the table key occurs at the start of the table are particularly suitable for control level processing.

The following diagram illustrates control level processing in a sorted table, where different field contents in the first three fields are indicated by different colors:

This graphic is explained in the accompanying text

Each change of color in a column indicates a control level change in the corresponding hierarchy level. Within the processing block of a loop, you can use the control level statement AT to react to a control level change. This enables you to restrict statements to a certain set of lines. You can thus use the SUMstatement to calculate totals from subsets of all lines.

The AT statement introduces a statement block that you end with the ENDAT statement.

AT level.
  statement block
ENDAT.

You can react to the following control level changes:

level

Meaning

FIRST

First line of the internal table

LAST

Last line of the internal table

NEW f

Beginning of a group of lines with the same contents in the field f and in the fields left of f

END Of f

End of a group of lines with the same contents in the field f and in the fields left of f

You can use control level statements to react to control breaks in internal tables instead of programming them yourself with logical expressions. Within the loop, you must order the AT-ENDATstatement blocks according to the hierarchy of the control levels. If the internal table itab has the columns f1, f2,.... and if it is sorted by these columns, you must program the loop as follows:

LOOP AT itab.
  AT FIRST.... ENDAT.
    AT NEW
f1....... ENDAT.
      AT NEW
f2....... ENDAT.
     .......
          single record processing
     .......

      AT END OF
f2.... ENDAT.
    AT END OF
f1.... ENDAT.
  AT LAST..... ENDAT.
ENDLOOP.

The innermost hierarchy level single line processing processes the table lines that do not correspond to a control level change. You do not have to use all control level statements. But you must place the ones that you use in the above sequence. You should not use control level statements in loops where the line selection is restricted by WHERE or FROM and TO. Neither should the table be modified during the loop.

If a control level field f1, f2,.... is not known until runtime, you can specify it dynamically as (n1), (n2), … where n1, n2,.... contains the field of f1, f2,.... . If n1, n2,....  is empty at runtime, the criterion for changing the control level is ignored. You can further restrict the group level fields  f1, f2,.... to partial fields by specifying offset and length.

If you are working with a work area wa, it does not contain the current line in the AT-ENDATstatement block. All character fields to the right of the current group key are filled with asterisks (*). All other fields to the right of the current group key contain their initial value.

Within an AT-ENDAT block, you can calculate the contents of the numeric fields of the corresponding control level using the SUMstatement.

.

You can only use this statement within a LOOP. If you use SUM in an AT-ENDAT block, the system calculates totals for the numeric fields of all lines in the current line group and writes them to the corresponding fields in the work area (see example in  ). If you use the SUM statement outside an AT-ENDAT block (single entry processing), the system calculates totals for the numeric fields of all lines of the internal table in each loop pass and writes them to the corresponding fields of the work area. It therefore only makes sense to use the SUM statement in AT-ENDAT blocks.

If the table contains a nested table, you cannot use the SUMstatement. Neither can you use it if you are using a field symbol instead of a work area in the LOOP statement.

Example

REPORT demo_int_tables_at_1.

DATA: BEGIN OF line,
         col1(1) TYPE c,
         col2 TYPE i,
         col3 TYPE i,
      END OF line.

DATA itab LIKE HASHED TABLE OF line
          WITH UNIQUE KEY col1 col2.

line-col1 = 'A'.
DO 3 TIMES.
  line-col2 = sy-index.
  line-col3 = sy-index ** 2.
  INSERT line INTO TABLE itab.

ENDDO.

line-col1 = 'B'.
DO 3 TIMES.
  line-col2 = 2 * sy-index.
  line-col3 = ( 2 * sy-index ) ** 2.
  INSERT line INTO TABLE itab.

ENDDO.

SORT itab.

LOOP AT itab INTO line.
  WRITE: / line-col1, line-col2, line-col3.
  AT END OF col1.
    SUM.
    ULINE.
    WRITE: / line-col1, line-col2, line-col3.
    SKIP.
  ENDAT.
  AT LAST.
    SUM.
    ULINE.
    WRITE: / line-col1, line-col2, line-col3.
  ENDAT.
ENDLOOP.

The list output is:

A          1          1

A          2          4

A          3          9
________________________________

A          6         14

B          2          4

B          4         16

B          6         36
________________________________

B         12         56

________________________________

*         18         70

The program creates a hashed table itab, fills it with six lines, and sorts it. In the LOOP - ENDLOOP block, the work area LINE is output for each loop pass. The first field of the table key, col1, is used for control level processing. The total for all numeric fields is always calculated when the contents of col1 change and when the system is in the last loop pass.

Example REPORT demo_int_tables_at_2.

DATA: BEGIN OF line,
        carrid   TYPE sbook-carrid,
        connid   TYPE sbook-connid,
        fldate   TYPE sbook-fldate,
        custtype TYPE sbook-custtype,
        class    TYPE sbook-class,
        bookid   TYPE sbook-bookid,
      END OF line.

DATA itab LIKE SORTED TABLE OF line WITH UNIQUE KEY table_line.

SELECT carrid connid fldate custtype class bookid
       FROM sbook INTO CORRESPONDING FIELDS OF TABLE itab.

LOOP AT itab INTO line.

  AT FIRST.
    WRITE / 'List of Bookings'.
    ULINE.
  ENDAT.

    AT NEW carrid.
      WRITE: / 'Carrid:', line-carrid.
    ENDAT.

      AT NEW connid.
        WRITE: / 'Connid:', line-connid.
      ENDAT.

        AT NEW fldate.
          WRITE: / 'Fldate:', line-fldate.
        ENDAT.

          AT NEW custtype.
            WRITE: / 'Custtype:', line-custtype.
          ENDAT.

               WRITE: / line-bookid, line-class.

            AT END OF class.
              ULINE.
            ENDAT.

ENDLOOP.

In this example, the sorted internal table itab is filled with data from the database table SBOOK using the Open SQL statement SELECT. The sequence of the columns in the internal table defines the control level hierarchy. Since the table key is the entire line, the sort sequence and the control level hierarchy are the same. The sequence of the AT-ENDAT blocks within the LOOP - ENDLOOP statements is important.

The output looks something like this:

List of Bookings
Carrid: AA
Connid: 0017
Fldate: 1998/11/22
Custtype: B
00063509 C
00063517 C
...

______________________________________________
00063532 F
00063535 F
...

______________________________________________
Custtype: P
00063653 C
00063654 C
...

______________________________________________
00063668 F
00063670 F
...

______________________________________________
Fldate: 1998/29/11
Custtype: B
00064120 C
00064121 C
...

and so on.

Specifying the Index in Loops

When you process an internal table in a loop, you can specify the index of an index table to restrict the number of entries that are read:

LOOP AT itab result [FROM n1] [TO n2] condition.
  statement block
ENDLOOP.

The loop is processed like any internal table. Within the processing block, the system field sy-tabix contains the index of the current line.

You can use the additions FROM and TO to specify the indexes n1 and n2 of the first and last entries that you want to read. The FROM and TO options restrict the number of lines which the system has to read. The WHERE addition in the condition only prevents the result from being processed. However, all of the table lines are read. To improve performance, you should use the FROM and TO options as much as possible. It can be also beneficial under certain conditions to leave the loop with the CONTINUE or EXIT statement.

Example

REPORT demo_int_tables_loop_ind.

DATA: BEGIN OF line,
         col1 TYPE i,
         col2 TYPE i,
      END OF line.

DATA itab LIKE SORTED TABLE OF line WITH UNIQUE KEY table_line.

DO 30 TIMES.
  line-col1 = sy-index.
  line-col2 = sy-index ** 2.
  APPEND line TO itab.

ENDDO.

LOOP AT itab INTO line FROM 10 TO 25 WHERE col2 > 400.
  WRITE: / sy-tabix, line-col2.

ENDLOOP.

The list output is:

        21       441

        22       484

        23       529

        24       576

        25       625

The example fills a sorted table itab with four lines. In the loop, only the lines 10 to 25 are read. There is also a condition that the contents of  col2 must be more than 400.

 

 

Leaving content frame