ABAP Keyword Documentation → ABAP - Reference → Processing Internal Data → Internal Tables → Expressions and Functions for Internal Tables
table_exp - Table Expressions
Other versions:
7.31 | 7.40 | 7.54
Syntax
... itab[ itab_line ] ...
Effect
A table expression consists of an internal table itab
, followed directly by a row
(itab_line
) specified in square brackets [
]. The expression finds the specified row in the internal table and returns it as the result of the corresponding row type, which can be used as follows:
- Reader Positions
- A table expression can be specified in general expression positions and functional operand positions with an appropriate operand type. The result is used here as an operand. The type of the result cannot be controlled in these operand positions using constructor operators. A default value can be specified for rows not found.
- A table expression can be specified as a special
expression variant for the memory area in the statement
ASSIGN
.
- A table expression can be specified as an argument of the
table function
line_index
and the predicate functionline_exists
.
- Writer Positions
- A table expression can be specified as a writable expression in result positions. Once found, the row in question can be modified directly here.
The internal table itab
must be specified directly using its name, a field symbol, or a dereferenced data reference as described under
Reader Positions. In a table with
header line, the table body is addressed and not the header line.
The structure component selector -
can be used to access components of the row in question and direct chainings [ ... ][
... ] of multiple table expressions. Table expression cannot yet, however, be specified on the left side of
object component selector ->
.
If the specified row is not found, a handleable expression of the class CX_SY_ITAB_LINE_NOT_FOUND is raised in all operand positions, except when
- a default value is specified in the definition of the type of the result,
- a table expression is used in the statement
ASSIGN
, wheresy-subrc
is set to the value 4,
- when used in the predicate function
line_exists
, where the logical value "false" is returned,
- when used in the table function
line_index
, where the value 0 is returned.
Notes
- Unlike other syntax representations in the ABAP key word documentation, the "
[
" and "]
" characters are part of the syntax.
- In table expressions, the empty square brackets
[]
cannot be specified behinditab
. In other operand positions, these empty brackets distinguish the table body from header lines.
- Functions and constructor expressions cannot currently be specified for
itab
, but the table expressions shown under Chainings are possible.
- A table expression cannot be followed directly by a
specified offset/length
+off(len)
, but this is possible after a chaining whose final place is a suitable structure component after a structure component selector.
- Duplicate selections (multiple reads performed on the same row of an internal table in different expressions) must be avoided manually. In these cases, a selection should be made before the statement and the result referenced by a field symbol or reference variable.
- Each table expression can be view as a short form for a variant of the statement
READ TABLE
that enables reads to be performed on rows of internal tables in operand positions.
- Unlike
READ TABLE
, a table expression does not modify the value of the system fieldsy-tabix
.
- Like the statement
READ TABLE
, a table expression is a single row read. If multiple rows of an internal table are to be read, the statementLOOP
generally displays better performance than using table expressions in a loop.
- Mesh path expressions are a special form of table expression that can be used in exactly the same way as table expressions.
Example
The content of the component carrid
of the row of the internal table
carrier_tab is passed to the method get_spfli
. In this table, the component carrname
of the
secondary key name
has a specific value.
DATA carrier_tab TYPE HASHED TABLE OF scarr
WITH UNIQUE KEY carrid
WITH NON-UNIQUE SORTED KEY name COMPONENTS carrname.
SELECT * FROM scarr INTO TABLE @carrier_tab.
TRY.
DATA(flight_tab) = cl_demo_spfli=>get_spfli(
carrier_tab[ KEY name
COMPONENTS carrname = 'United Airlines' ]-carrid ).
cl_demo_output=>display( flight_tab ).
CATCH cx_sy_itab_line_not_found.
cl_demo_output=>display( `Nothing found` ).
ENDTRY.
Example
Here, the first calculation with table rows is a bad example of how to use table expressions. The same selection is made three times in the same statement. The second calculation shows how this can be avoided by using an assignment to a field symbol.
DATA itab TYPE TABLE OF i.
itab = VALUE #( ( 3 ) ( 5 ) ).
"Bad example
itab[ table_line = 3 ] =
itab[ table_line = 3 ] * itab[ table_line = 3 ].
"Good example
ASSIGN itab[ table_line = 5 ] TO FIELD-SYMBOL(<fs>).
<fs> = <fs> * <fs>.
Examples
The program DEMO_TABLE_EXPRESSIONS shows further examples of how to use table expressions.
Exceptions
Catchable Exceptions
CX_SY_ITAB_LINE_NOT_FOUND
-
Cause: The specified table row was not found.
Runtime Error:ITAB_LINE_NOT_FOUND