ABAP Keyword Documentation → ABAP − Reference → Processing Internal Data → Internal Tables → Expressions and Functions for Internal Tables → table_exp - Table Expressions
table_exp - Chainings
Table expressions can be chained with the structure
component selector -
, with other table expressions, and with the object component selector ->
.
If the constructor operator VALUE
or REF
is specified before a chaining to
control the result, only the final result is affected.
Any intermediate results are always temporary field symbols and the corresponding performance notes should be read.
Other versions:
7.31 | 7.40 | 7.54
Note
Chainings of table expressions with functional method calls (namely specifying the internal table as a functional method call meth( ... )[ ...
] or meth( ... )-comp[ ...
] and accessing the internal table using a chained attribute access meth( ... )->itab[ ... ]
) are not yet possible. Table expressions cannot currently be placed on the right of a method call in
method chainings.
Chainings with the Structure Component Selector
A table expression can be chained as follows using the structure component selector -
:
- If the result of a table expression
itab[ ... ]
is a structure, the structure component selector can be used to read each componentcomp
of the structure:
... itab[ ... ]-comp ...
comp
has a suitable type, a
specified offset/length
+off(len)
can be appended, except when the chaining is used as a
memory area of the statement ASSIGN
or as an argument of the reference operator REF
.
- If a structure
struct
has a table-like componentcomp
, it can be used as the name of the internal table of a table expression:
... struct-comp[ ... ] ...
Both of these alternatives can be combined, enabling a structured component selector to be located directly on the left and right of a table expression.
Note
If the reference operator REF
is specified in front of a chaining whose result is a component of a structured table row, it creates a reference to this component.
Example
Reads the first column of the second row of the table-like second component of the first row of the internal table itab
. The result is 4.
TYPES: BEGIN OF struct1,
col1 TYPE i,
col2 TYPE i,
END OF struct1,
tab1 TYPE TABLE OF struct1 WITH EMPTY KEY,
BEGIN OF struct2,
col1 TYPE i,
col2 TYPE tab1,
END OF struct2,
tab2 TYPE TABLE OF struct2 WITH EMPTY KEY.
DATA(itab) =
VALUE tab2(
( VALUE struct2( col1 = 1
col2 = VALUE tab1(
( VALUE struct1( col1 = 2
col2 = 3 ) )
( VALUE struct1( col1 = 4
col2 = 5 ) ) ) ) )
( VALUE struct2( col1 = 6
col2 = VALUE tab1(
( VALUE struct1( col1 = 7
col2 = 8 ) )
( VALUE struct1( col1 = 9
col2 = 10 ) ) ) ) ) ).
DATA(num) = itab[ 1 ]-col2[ 2 ]-col1.
Executable Example
Chainings with Table Expressions
If the result of a table expression itab[ ... ]
is an internal table, it can be used as the name of the internal table of a table expression:
... itab[ ... ][ ... ] ...
An expression of this type can
- It can itself be continued with square brackets
[ ... ]
, if its result is again table-like.
- combined with the information from structure component selectors above.
Example
In an internal table nested twice, reads the first row of the innermost table in the second row of the center table in the second row of the outermost table. The result is 7.
TYPES: tab1 TYPE TABLE OF i WITH EMPTY KEY,
tab2 TYPE TABLE OF tab1 WITH EMPTY KEY,
tab3 TYPE TABLE OF tab2 WITH EMPTY KEY.
DATA(itab) = VALUE tab3(
( VALUE tab2(
( VALUE tab1(
( 1 )
( 2 ) ) )
( VALUE tab1(
( 3 )
( 4 ) ) ) ) )
( VALUE tab2(
( VALUE tab1(
( 5 )
( 6 ) ) )
( VALUE tab1(
( 7 )
( 8 ) ) ) ) ) ).
DATA(num) = itab[ 2 ][ 2 ][ 1 ].
Example
Direct chaining of table expressions and chaining with the structure component selector in a single expression. Reads the structure component of the innermost nested table with the value 13.
TYPES: BEGIN OF struct1,
col1 TYPE i,
col2 TYPE i,
END OF struct1,
tab1 TYPE TABLE OF struct1 WITH EMPTY KEY,
BEGIN OF struct2,
col1 TYPE i,
col2 TYPE TABLE OF tab1 WITH EMPTY KEY,
END OF struct2,
tab2 TYPE TABLE OF struct2 WITH EMPTY KEY.
DATA(itab) =
VALUE tab2(
( VALUE struct2(
col1 = 1
col2 = VALUE #(
( VALUE tab1(
( VALUE struct1(
col1 = 2
col2 = 3 ) )
( VALUE struct1(
col1 = 4
col2 = 5 ) ) ) )
( VALUE tab1(
( VALUE struct1(
col1 = 6
col2 = 7 ) )
( VALUE struct1(
col1 = 8
col2 = 9 ) ) ) ) ) ) )
( VALUE struct2(
col1 = 10
col2 = VALUE #(
( VALUE tab1(
( VALUE struct1(
col1 = 11
col2 = 12 ) )
( VALUE struct1(
col1 = 13
col2 = 14 ) ) ) )
( VALUE tab1(
( VALUE struct1(
col1 = 15
col2 = 16 ) )
( VALUE struct1(
col1 = 17
col2 = 18 ) ) ) ) ) ) ) ).
DATA(num) = itab[ 2 ]-col2[ 1 ][ 2 ]-col1.
Chainings with the Object Component Selector
If the result of a table expression or one of the preceding chainings has a reference type, these chainings can be placed directly in front of the object component selector to access the components of the referenced object:
... itab[ ... ]->comp ...
... itab[ ... ]-comp->comp ...
... struct-comp[ ... ]->comp ...
... itab[ ... ][ ... ]->comp ...
A component addressed in this way can, depending on its design, be used in all suitable operand positions and notably in further chainings.
Notes
- More specifically, the dereferencing operator
->*
can be specified after table expressions.
- An instance method call can be a
standalone call or a
functional method call. The obsolete form
of the statement
CALL METHOD
cannot be used.
- No table expressions can be specified in a writing position for a read-only internal table. This also applies when the object component selector is used to access another object. As before, standalone method calls a not possible using a read-only internal table.
Example
Dereferences a row of an internal table of data reference variables and accesses a single component.
DATA itab TYPE TABLE OF REF TO scarr WITH EMPTY KEY.
itab = VALUE #( ( NEW scarr( carrid = 'XX' carrname = 'YYYY' ) ) ).
cl_demo_output=>new(
)->write_data( itab[ 1 ]->*
)->write_data( itab[ 1 ]->carrid
)->write_data( itab[ 1 ]->carrname )->display( ).
Example
Fills an internal table of reference variables with object references and accesses attributes and methods of the objects using table expressions in front of the object component selector.
CLASS number DEFINITION.
PUBLIC SECTION.
DATA value TYPE i.
METHODS:
get RETURNING VALUE(r) TYPE i,
constructor IMPORTING n TYPE i.
ENDCLASS.
CLASS number IMPLEMENTATION.
METHOD get.
r = value.
ENDMETHOD.
METHOD constructor.
value = n.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
TYPES itab TYPE TABLE OF REF TO number WITH EMPTY KEY.
DATA(itab) = VALUE itab( FOR j = 1 UNTIL j > 2
( NEW number( j ) ) ).
data(n1) = itab[ 1 ]->value.
data(n2) = itab[ 2 ]->get( ).