ABAP Keyword Documentation → ABAP − Reference → Processing Internal Data → Internal Tables → Expressions and Functions for Internal Tables → FOR - Table Iterations
FOR ... IN itab
Other versions:
7.31 | 7.40 | 7.54
Syntax
... FOR wa|<fs> IN itab [INDEX INTO idx] [
cond] [let_exp] ...
Addition
Effect
This variant of an iteration expression for
table iterations using
FOR
evaluates an internal table itab
sequentially, like a
row variant of a LOOP
. itab
is a
functional operand
position. As in LOOP
,
the order of the rows read is determined by the table category or a key specified in cond
.
With cond1
you can set conditions
for the evaluation. For each read row, the result is either assigned to a local work area wa1
or to a field symbol <fs>
. The work area or the field symbol is declared
implicitly with the row type of the internal table and bound locally to the FOR
expression as a subexpression of the full constructor expression. The same applies to the namespace
and visibility as to the helper fields declared in LET
expressions. After the FOR
expression, the work area or the field symbol can either be used in further subexpressions or to construct the result of a
table comprehension or table reduction.
Notes
- Instead of using directly specified internal tables
itab
, this variant ofFOR
expressions can also be created using mesh paths, whereFOR
expressions from both categories can be used together in the same constructor expression.
- Changes to the content of the internal table specified after
IN
within theFOR
expression can only be made using method calls. Deleting or replacing the table in full always produces a runtime error.
- As in
LOOP
, there is no addition for reversing the order in which the rows are read. A conditional iteration must be used instead (see the executable example).
Example
Constructs an internal table flights
in a table iteration with FOR
with the value operatorVALUE
(table comprehension).
The rows of the internal table merge the content of two existing internal tables scarr_tab
and spfli_tab
.
SELECT carrid, carrname
FROM scarr
INTO TABLE @DATA(scarr_tab).
SELECT carrid, connid, cityfrom, cityto
FROM spfli
ORDER BY carrid, connid, cityfrom, cityto
INTO TABLE @DATA(spfli_tab).
TYPES:
BEGIN OF flight,
carrier TYPE scarr-carrname,
number TYPE spfli-connid,
departure TYPE spfli-cityfrom,
destination TYPE spfli-cityto,
END OF flight,
flights TYPE TABLE OF flight WITH EMPTY KEY.
DATA(flights) =
VALUE flights( FOR <fs> IN spfli_tab
( carrier = VALUE #(
scarr_tab[ carrid = <fs>-carrid ]-carrname
DEFAULT '???' )
number = <fs>-connid
departure = <fs>-cityfrom
destination = <fs>-cityto ) ).
cl_demo_output=>display( flights ).
Executable Examples
Addition
... INDEX INTO idx
Effect
For each row of the associated FOR
expression, this addition sets the helper
variable idx
to the value to which the system field sy-tabix
would be set in a corresponding LOOP
.
The helper variable idx
is declared implicitly with the type i
and bound locally to the FOR
expression as a subexpression of the full constructor
expression. The same applies to the namespace and visibility as to the helper fields declared in LET
expressions.
Example
Constructs an internal table carriers
from an existing internal table scarr_tab
. The first column is provided with the row index of the existing table.
SELECT carrid, carrname
FROM scarr
INTO TABLE @DATA(scarr_tab).
TYPES:
columns LIKE LINE OF scarr_tab,
BEGIN OF carrier,
no TYPE i.
INCLUDE TYPE columns.
TYPES:
END OF carrier,
carriers TYPE TABLE OF carrier WITH EMPTY KEY.
DATA(carriers) =
VALUE carriers( FOR wa IN scarr_tab INDEX INTO i
( no = i carrid = wa-carrid carrname = wa-carrname ) ).
cl_demo_output=>display( carriers ).