Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Processing Internal Data →  Internal Tables →  Processing Statements for Internal Tables →  LOOP AT itab →  LOOP AT itab - Basic Form 

LOOP AT itab - result

Quick Reference

Other versions: 7.31 | 7.40 | 7.54

Syntax


... { INTO wa } 
  | { ASSIGNING <fs> [CASTING] }
  | { REFERENCE INTO dref }
  | { TRANSPORTING NO FIELDS } ...

Effect

Defines the output behavior of a LOOP across an internal table. There are four alternatives for the output behavior:

  • The addition INTO is used to assign the content of the current row to a work area wa.
  • The addition ASSIGNING is used to assign the current row to a field symbol <fs>. No other memory area can be assigned to the field symbol within the loop and the assignment cannot be undone using UNASSIGN.
  • The addition REFERENCE INTO is used to create a reference to the current row in a reference table. No other reference can be assigned to the reference variable within the loop and the reference variable cannot be initialized using CLEAR.
  • The addition TRANSPORTING NO FIELDS specifies that only the relevant system fields are filled. This addition is only possible if the addition WHERE is used in the conditions cond at the same time and if the addition GROUP BY is not used.

If the internal table itab is specified as an existing data object, the syntax and meaning of the specified output behavior is the same as in the statement READ TABLE (with the exception that no further transport_options can be specified after INTO wa) and the same restrictions apply to modifications to key fields of the primary and secondary table keys. In particular, inline declarations for the work area, the field symbol, or the reference variable using the declaration operators DATA and FIELD-SYMBOL are possible.

If a field symbol or a dereferenced reference variable is specified for the work area wa or the reference variable dref, the target data object of the field symbol or reference variable is determined in each loop pass. The current data object is used as the target area for each loop pass. This means that changes in the assignment of the field symbol or reference variable within the loop modifies the target area.

If the internal table is specified as the return value of a functional method, a constructor expression, or a table expression, the additions ASSIGNING and REFERENCE INTO can also be specified for LOOP (this is not the case with READ TABLE). The internal table is only available while the loop is being processed, which means that all field symbols and reference variables that point to rows in the internal table become invalid when the loop is exited.

Programming Guideline

Output Behavior


Notes

  • If the current row is deleted within the loop when the additions ASSIGNING or REFERENCE are used, the field symbol or reference variable are then unassigned or unbound in the current loop pass.
  • For LOOP, an obsolete short form exists (outside of classes) where INTO wa can be omitted if the internal table has a header line itab with the same name. INTO itab is then added to the statement.

Example

Two LOOP loops are used to determine the row numbers for numbers greater than or equal to 40 and less than or equal to 60 in a sorted table of random numbers, and the addition TRANSPORTING NO FIELDS is used. Then, INTO is used to read these rows to a variable number. Filling the output table in this way demonstrates the use of number. It could also be done using INSERT LINES OF.

DATA(rnd) = cl_abap_random_int=>create( seed = + sy-uzeit 
                                      min  = 1 
                                       max  = 100 ). 

DATA itab TYPE SORTED TABLE OF i WITH NON-UNIQUE KEY table_line. 
itab = VALUE #( FOR i = 1 UNTIL i > 100 ( rnd->get_next( ) ) ). 

LOOP AT itab TRANSPORTING NO FIELDS WHERE table_line >= 40. 
  DATA(idx1) = sy-tabix. 
  EXIT. 
ENDLOOP. 

LOOP AT itab FROM idx1 TRANSPORTING NO FIELDS WHERE table_line > 60. 
  DATA(idx2) = sy-tabix - 1. 
  EXIT. 
ENDLOOP. 

DATA output TYPE TABLE OF i WITH EMPTY KEY. 
LOOP AT itab FROM idx1 TO idx2 INTO DATA(number). 
  output = VALUE #( BASE output ( number ) ). 
ENDLOOP. 
cl_demo_output=>display( output ).

Example

Loop across an internal table containing dates. The table rows are assigned to a structured field symbol with a corresponding casting.

TYPES: 
  BEGIN OF date, 
    year  TYPE c LENGTH 4, 
    month TYPE c LENGTH 2, 
    day   TYPE c LENGTH 2, 
  END OF date. 

DATA itab TYPE SORTED TABLE OF d WITH UNIQUE KEY table_line. 
itab = VALUE #( FOR i = - 3 UNTIL i > 3 ( sy-datlo + i ) ). 

DATA output TYPE TABLE OF date WITH EMPTY KEY. 
FIELD-SYMBOLS <date> TYPE date. 
LOOP AT itab ASSIGNING <date> CASTING. 
  output = VALUE #( BASE output ( <date> ) ). 
ENDLOOP. 
cl_demo_output=>display( output ).