LOOP AT itab.
LOOP AT itab INTO wa.
1. ... FROM n1
2. ... TO n2
3. ... WHERE logexp
4. ... TRANSPORTING NO FIELDS
Processes an internal table ( DATA)
in a loop which begins with LOOP and ends with ENDLOOP. Each of the
internal table entries is sent to the output area in turn.
When LOOP AT itab. is used, the header line of the internal table itab is used as output area. In the case of LOOP AT itab INTO wa, there is an explicitly specified work area wa.
If the internal table is empty, all the statements between LOOP and ENDLOOP are ignored.
The sequence in which the entries occur in the loop depend on the table type specified in the table definition (unless the table has been explicitly resorted using the SORT statement:
The system processes entries according to their logical index. At the beginning of a loop pass, SY-TABIX is set to the logical index of the current table entry.
The system processes entries according to their sort order. As with standard tables, the system sets SY-TABIX to the logical index of the current table entry.
The system processes entries in the order in which they were inserted into the table. Since hashed tables have no logical index, SY-TABIX is always set to 0.
When the system exits a LOOP, the index field SY-TABIX always has the same value it had before the loop was processed.
Inserting and/or deleting lines in a LOOP affects subsequent loop passes.
For control break processing in a LOOP on internal tables, there are special control break control structures for internal tables you can use.
You can use the CONTINUE statement to leave the current loop pass prematurely and continue with the next loop pass. To leave loop processing altogether, you use EXIT.
At the end of loop processing (i.e. after ENDLOOP), the return code value of SY-SUBRC specifies whether the loop was actually processed.
SY-SUBRC = 4:
The loop was not executed, either because there was no entry at all or because there was no entry which satisfied the conditions.
DATA: BEGIN OF STRUC, NAME(10) TYPE C, BLNCE TYPE P, END OF STRUC, ITAB LIKE TABLE OF STRUC.
After the table has been filled with data (using APPEND), it is then output:
LOOP AT ITAB INTO STRUC. WRITE: / STRUC-NAME, STRUC-BLNCE. ENDLOOP.
If an internal table is processed only on a restricted basis
(with the additions FROM, TO and/or WHERE), you should not use the
structures for control break processing because the
interaction of a restricted LOOP and the AT statement is undefined
at present. (In AT NEW or AT END OF, the system compares with the
previous table lines, and not with the selected ones. A restricted
LOOP in this case may not catch a change of control level.)
Places all internal table entries from the entry with the index
(SY-TABIX) = n1 to the entry with the index = n2 inclusive
in the output area in turn.
You cannot use these addtions for HASHED TABLEs, because hashed tables hav eno defined index operations.
If either one of the additions "FROM n1" or "TO n2 " is missing, then the table is processed either from the first entry or up to the last entry (according to what is missing).
LOOP AT ITAB INTO STRUC FROM 7 TO 8. WRITE: / STRUC-NAME, STRUC-BLNCE. ENDLOOP.
Places all internal table entries which satisfy the condition logexp in turn in the output area. The condition logexp can be almost any logical expression. The only restriction is that the first field for each comparison must be a sub-field of the line structure of the internal table itab.
LOOP AT ITAB INTO STRUC WHERE BLNCE <> 0. WRITE: STRUC-NAME, STRUC-BLNCE. ENDLOOP.
which has the same effect as:
LOOP AT ITAB INTO STRUC. CHECK STRUC-BLNCE <> 0. WRITE: / STRUC-NAME, STRUC-BLNCE. ENDLOOP.
LOOP AT INTTAB WHERE TABLE LINE > 5 AND
TABLE LINE <= 10.
When you work with STANDARD or HASHED tables, the WHERE
condition is processed by checking all table entries (full table
scan). When you use SORTED tables, you can optimize the partial
sequential processing of table entries internally such that only
the entries affected by the WHERE condition are processed. In order
to optimize the process in this way, you must specify the WHERE
condition in the form:
WHERE k1 = v1 AND k2 = v2 AND ... AND kn = vn
The components k1, ..., kn must occur at the beginning of the table key.
The operands occurring in the logical expression in the WHERE condition are only analyzed once, when the loop processing begins. If their values change within the loop itself, this does not change the WHERE condition.
sense, that is, a logical expression as in IF, WHILE or CHECK, and not a WHERE clause in the SQL sense as in SELECT/UPDATE/DELETE... WHERE.
The interaction between the LOOP AT ... WHERE statement and the AT control break statements is currently undefined. It is therefore important to avoid using either the AT NEW/END OF or FIRST/LAST statements in a LOOP loop with a WHERE condition.
DATA: CMP_NAME LIKE STRUC_NAME: CMP_NAME = 'Harold'. LOOP AT ITAB INTO STRUC WHERE NAME = CMP_NAME. WRITE: / STRUC-NAME, STRUC-BLNCE. ENDLOOP.
There is no field transport in the output area of the internal
table. This addition can be used only in conjunction with a WHERE
condition. Since it would make no sense to specify a work area with
INTO wa when using the addition TRANSPORTING NO FIELDS , this
option does not exist.
This addition can be used to determine a set of line indexes (index set) or to determine the number of lines in a table which satisfy a given condition.
Determining the number COUNT of lines in a name table
TAB which contain the name 'Walter' and the corresponding
index set INDEX_SET.
DATA: BEGIN OF NAMETAB OCCURS 100, NAME(30) TYPE C, END OF NAMETAB, COUNT TYPE I, INDEX_SET LIKE SY-TABIX OCCURS 10 WITH HEADER LINE. LOOP AT NAMETAB TRANSPORTING NO FIELDS WHERE NAME CS 'Walter'. INDEX_SET = SY-TABIX. APPEND INDEX_SET. ADD 1 TO COUNT. ENDLOOP.
APPEND , COLLECT, INSERT, MODIFY, DELETE, SORT,
AT NEW/END OF/FIRST/LAST, READ TABLE.