ABAP Keyword Documentation → ABAP − Reference → Processing Internal Data → Internal Tables → Expressions and Functions for Internal Tables → table_exp - Table Expressions
table_exp - default
Other versions:
7.31 | 7.40 | 7.54
Syntax
... OPTIONAL|{DEFAULT def} ...
Effect
Specifies a default value for table rows not found. if the
type of the result of a table expression
table_exp is defined using the value operator VALUE
or the reference operator REF, a default value can be specified after the
table expression. If the row itab_line
specified in the table expression is not found, an exception of the class CX_SY_ITAB_LINE_NOT_FOUND is not raised if a default value is specified and the result is determined by the default value instead.
OPTIONAL
- If
VALUEis used, the default value is an initial data object with the data type of the table expression.
- If
REFis used, the default value is an initial reference variable with the static type of the table expression.
DEFAULT
- If
VALUEis used, the default value is specified using a data objectdef, which must be convertible to the data type of the table expression, Ifdefis not compatible with the data type of the result of the expression, a conversion is made to this data type in accordance with the conversion rules.
- If
REFis used, the default value is specified using a reference variabledef, whose static type is the data type of the table expression.
def is a general expression position.
If the argument of VALUE or REF is a single table expression, the default value applies to a single row in the table in question. If the argument is a
chaining of table expressions, the default value
applies to the result of the chaining (namely how it ends on the right, expressing either a structure
component or a table row). A default value specified explicitly must match the result of the chaining
accordingly. When a default value is specified for a chaining, the first exception for a missing row across the entire chaining is caught and the default value is returned as a result.
Notes
- Alongside the functions
line_existsandline_function, a characteristic default value specified for missing rows also enables the existence of rows to be checked without catching an exception or checking the return value after the statementASSIGN.
- A default value
defspecified afterDEFAULTcan itself be a table expression with default value. This enables alternative searches to be performed within an expression.
- There is no operator for the default behavior of a table expression in which the result is usually a temporary field symbol, which means that no default value can be specified here.
Example
If no row of the specified key is found, the table expression returns a row in which the component carrid has the value "NUL" and all other components are initial.
DATA spfli_tab TYPE SORTED TABLE OF spfli
WITH UNIQUE KEY carrid connid.
SELECT *
FROM spfli
INTO TABLE @spfli_tab.
cl_demo_output=>display( VALUE #( spfli_tab[ carrid = '...'
connid = '...' ]
DEFAULT VALUE #( carrid = 'NUL' ) ) ).
Example
This example demonstrates the difference between specifying the default value for the value operator
VALUE and for the reference operator REF. Once
this source code section is executed, var1 is an initial structure with the
type SCARR and dref1 is an initial reference variable with the static type
of this structure. The reference variable dref1 does not point to an initial
structure. In the case of the default value specified using DEFAULT, a reference
variable must be specified for REF. This is done here using the instance operator NEW, not VALUE.
DATA itab TYPE TABLE OF scarr WITH EMPTY KEY.
DATA(var1) = VALUE #( itab[ 1 ] OPTIONAL ).
DATA(var2) = VALUE #( itab[ 1 ] DEFAULT VALUE #( carrid = 'XXX'
carrname = 'xxx' ) ).
DATA(dref1) = REF #( itab[ 1 ] OPTIONAL ).
DATA(dref2) = REF #( itab[ 1 ] DEFAULT NEW #( carrid = 'XXX'
carrname = 'xxx' ) ).
ASSERT var1 IS INITIAL.
ASSERT dref1 IS INITIAL.
ASSERT var2 = dref2->*.
Executable Example
Table Expressions, Default Value