AS ABAP Release 758, ©Copyright 2024 SAP SE. All rights reserved.
ABAP - Keyword Documentation → ABAP - Programming Language → Processing Internal Data → Internal Tables (itab) → itab - Expressions and Functions → itab - Table Expressions (table_exp) →table_exp - default
Syntax
... OPTIONAL|{DEFAULT def} ...
Effect
Specifies a default value for table lines 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 line 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.
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 line in the corresponding table. If the argument is a chaining of table expressions, the default value applies to the result of the chaining, that is, to its right end, which can express a structure component or a table line. 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 line across the entire chaining is caught and the default value is returned as a result.
Hints
Example
If no line of the specified key is found, the table expression returns a line 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 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.
FINAL(var1) = VALUE #( itab[ 1 ] OPTIONAL ).
FINAL(var2) = VALUE #( itab[ 1 ] DEFAULT VALUE #( carrid = 'XXX'
carrname = 'xxx' ) ).
FINAL(dref1) = REF #( itab[ 1 ] OPTIONAL ).
FINAL(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