Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Processing Internal Data →  Internal Tables →  Processing Statements for Internal Tables 

LOOP AT itab

Short Reference

Other versions: 7.31 | 7.40 | 7.54


LOOP AT itab result [cond]. 


The LOOP and ENDLOOP statements define a loop around a statement block. The LOOP statement reads rows from the internal table itab sequentially.

  • The output response result determines how and to where the row contents are read.
  • The table key with which the loop is executed can be determined in cond. You can either read all the rows or specify cond conditions to restrict which rows to read.

The statement block between LOOP and ENDLOOP is executed once for each row. To exit processing of the statement block, you can use the statements described in the leave loops section.

If no explicit table key name is declared after USING KEY, the order in which the rows are read depends on the table type as follows:

  • Standard tables and sorted tables
    The rows are read by ascending row numbers in the primary table index. In each loop pass, the system field sy-tabix contains the row number of the current row in the primary table index.
  • Hashed Tables
    The rows are processed in the order in which they were inserted in the table, and by the sort sequence with the SORT statement. In each loop pass, the system field sy-tabix contains the value 0.

The loop continues to run until all the table rows that meet the cond condition have been read or until it is exited with a statement. If no appropriate rows are found or if the internal table is blank, the loop is not run at all.

System Fields

During each loop run for index tables, and when you are using a sorted key, the statement LOOP AT sets the value of the system field sy-tabix to the row number of the current row in the relevant table index. In hashed tables and when using a hash key sy-tabix is set to the value 0. LOOP AT does not modify sy-subrc. After leaving the loop using ENDLOOP, sy-tabix is set to the value that it had before entering the loop and that applies for sy-subrc:

sy-subrc Meaning
0 The loop was run at least once.
4 The loop was not run at all.

The system fields sy-tfill and sy-tleng are also filled.

Changing internal tables in a loop

If you insert or delete rows in the statement block of a LOOP, this will have the following effects: The position of inserted or deleted rows with regard to the current row is determined by the row numbers in the corresponding table index in the case of loops on index tables or if using a sorted key. In the case of loops on hashed tables and if using a hash key, the position depends on the insertion sequence.

  • If you insert rows after the current row, these new rows will be processed in the subsequent loop passes. An endless loop can result.
  • If you delete rows after the current row, the deleted rows will no longer be processed in the subsequent loop passes.
  • If you insert rows before the current row, the internal loop counter is increased by one with each inserted row. This affects sy-tabix, which is also increased (in the subsequent loop pass in the case of loops on index tables or when using a sorted key).
  • If you delete the current row or rows before the current row, the internal loop counter is decreased by one with each deleted row. In the case of loops on index tables or if using a sorted key, this affects sy-tabix in the subsequent loop pass, and sy-tabix is decreased accordingly.

The replacement of the entire table body in a LOOP using this table causes the loop to be exited at the next loop pass in accordance with the rules described above. This is particularly the case if new rows were added to the table afterwards. Since this usually leads to unpredictable program behavior, you cannot access the entire table body in change mode in a loop. If this is statically discernible, a syntax error occurs in classes and for LOOPS with statically discernible secondary keys. Otherwise, the syntax check simply returns a warning for compatibility reasons. However, at runtime, a runtime error always occurs in the case of the replacement of the entire table body with statements such as CLEAR, FREE, LOCAL, REFRESH, SORT, DELETE ... WHERE, and with all types of assignments to itab.

Programming Guideline

Loop Processing


  • If the internal table itab is specified using a reference variable, the loop is executed completely using the table referenced at entry. Any changes to the reference variable do not have an effect on the loop. The associated object cannot be deleted from the Garbage Collector until the loop has been completed. The same thing is true if the table is represented by a field symbol. After the implementation of the field symbol in the loop, iteration still takes place using the table linked to the field symbol when LOOP is entered.
  • There is no implicit selection of a suitable key or index. The used table key or table index is always specified uniquely. The syntax check issues a warning if there is a suitable secondary table key but this table key is not used. This warning should be removed through using the key. However, in exceptional cases, it can be bypassed using a pragma.


Nested LOOP loops without explicit key declaration. The contents of the current row for the outer loop are analyzed in the WHERE condition for the inner loop.

PARAMETERS p_name TYPE scarr-carrname DEFAULT '*'. 

DATA: scarr_tab TYPE SORTED TABLE OF scarr 
                WITH UNIQUE KEY carrname, 
      spfli_tab TYPE SORTED TABLE OF spfli 
               WITH NON-UNIQUE KEY carrid. 

FIELD-SYMBOLS <scarr_line> LIKE LINE OF scarr_tab. 
DATA spfli_line LIKE LINE OF spfli_tab. 

       FROM scarr 
       INTO TABLE scarr_tab. 

       FROM spfli 
       INTO TABLE spfli_tab. 

LOOP AT scarr_tab ASSIGNING <scarr_line> 
                  WHERE carrname CP p_name. 
  LOOP AT spfli_tab INTO spfli_line 
                   WHERE carrid = <scarr_line>-carrid. 
    WRITE: / spfli_line-carrid, 


Catchable Exceptions


  • Cause: Error in a dynamic WHERE condition
    Runtime Error: DYN_WHERE_PARSE_ERROR

Non-Catchable Exceptions

  • Cause: Illegal conversion of the LOOP field symbol in the core of the loop.
    Runtime Error: ITAB_ILLEGAL_REG
  • Cause: Illegal assignment to the LOOP reference in the core of the loop.
    Runtime Error: MOVE_TO_LOOP_REF
  • Cause: Unpermitted change of entire table body in the loop
    Runtime Error: TABLE_FREE_IN_LOOP


LOOP AT itab - result

LOOP AT itab - cond


Internal Tables, Loop with Key Specified