SAP TABLE EXPRESSIONS



Get Example source ABAP code based on a different SAP table
  


• [ ] ABAP_TABLE_EXPR

ABAP_ITAB - Table Expressions ( ABAP_ITABEXP )

ABAP_SYNTAX
... itab[ itab_line ] $[-comp$|[ ... ]$|->comp$] ...

What does it do?
A table expression consists of an internal table itab directly followed by a line specification itab_line in square brackets [ ]. A chaining -comp$|[ ... ]$|->comp can be appended to this line. The expression searches for the specified line in the internal table.
If no chaining is specified, the entire line is returned as the result of the corresponding line type.
If a chaining is specified, the following is possible:
The structure component selector - can be used to access the component comp in the read line.
Square brackets [ ... ] can be used to chain multiple table expressions.
The object component selector -> can be used to access a component comp of the object referenced by the preceding expression.
The result of a table expression can be used as follows:
Read positions
A table expression can be specified in general expression positions and functional operand positions with a suitable 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 lines that are 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 function line_exists.
Write positions
A table expression can be specified as a writable expression in result positions. The line found 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 Read Positions.
BEGIN_SECTION VERSION 5 OUT In a table with header line, the table body is addressed and not the header line.
END_SECTION VERSION 5 OUT
If the specified line is not found, a catchable exception 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, where sy-subrc is set to the value 4,
used in the predicate function line_exists, where the truth value false is returned,
used in the table function line_index, where the value 0 is returned.



Latest notes:

In table expressions, the empty square brackets [] cannot be specified after itab. 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 after a chaining whose final place is a suitable structure component after a structure component selector.
Duplicate selections, that is, multiple reads performed on the same line of an internal table in different expressions, must be avoided in a statement or a loop with multiple table expressions. In these cases, a selection should be made before the statement or the loop and the result referenced by a field symbol or reference variable.
The exception class CX_SY_ITAB_LINE_NOT_FOUND contains attributes for displaying the line number in the index or key used if an access fails. If only index accesses are used in statements with multiple table expressions, it is not possible to distinguish which expression was unsuccessful.
Each table expression can be viewed as a short form for a variant of the statement READ TABLE that enables reads to be performed on lines of internal tables in operand positions.
In write positions, the same restrictions apply with respect to modifying key fields to table expressions whose result is a field symbol or temporary reference variable as to other field symbols or data reference variables that point to lines of internal tables. In particular, table expressions that return a line of a sorted table or hashed table to write positions like the left side of an assignment or as actual parameters for output parameters always raise an exception.
A table expression cannot be specified in a write position if it is a read-only table, such as an input parameter passed by reference. This restriction applies even if a chaining is used and the content of the table itself is not actually modified.
Unlike READ TABLE, a table expression does not change the value of the system field sy-tabix, except when used in the statement ASSIGN.
Like the statement READ TABLE, a table expression is a single line read. If multiple lines of an internal table are to be read, the statement LOOP or a FOR expression generally has 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.
NON_V5_HINTS
ABAP_HINT_END

ABAP_EXAMPLE_VX5
The content of the component carrid of the line 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.
ABEXA 01329
ABAP_EXAMPLE_END

ABAP_EXAMPLE_VX5
The first calculation with table lines 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 .
ABEXA 01330
ABAP_EXAMPLE_END

VX_EXA_ONLY

ABAP_EXAMPLES
The program DEMO_TABLE_EXPRESSIONS shows further examples of how to use table expressions.
ABAP_EXAMPLE_END




Runtime Exceptions



Catchable Exceptions
CX_SY_ITAB_LINE_NOT_FOUND
Reason for error:
The specified table line was not found.
Runtime error:
ITAB_LINE_NOT_FOUND