Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Processing Internal Data →  Internal Tables →  Expressions and Functions for Internal Tables →  table_exp - Table Expressions →  table_exp - default 

Table Expressions, Default Value

This example demonstrates default values for table expressions.

Other versions: 7.31 | 7.40 | 7.54

Source Code

    TYPES:
      BEGIN OF line,
        id    TYPE i,
        value TYPE string,
      END OF line,
      itab TYPE SORTED TABLE OF line WITH UNIQUE KEY id.

    DATA(def) = VALUE line( id = 0 value = `not found` ).

    DATA(itab) = VALUE itab( ( id = 3 value = `CCC` )
                             ( id = 4 value = `DDD` )
                             ( id = 5 value = `EEE` ) ).

    DATA(result1) = VALUE #( itab[ id = 1 ] DEFAULT def ).
    cl_demo_output=>write( result1 ).


    DATA(result2) = VALUE #( itab[ id = 1 ]-value DEFAULT def-value ).
    cl_demo_output=>write_data( result2 ).

    DATA(result3) = VALUE #( itab[ id = 1 ] DEFAULT VALUE #(
                             itab[ id = 2 ] DEFAULT VALUE #(
                             itab[ id = 3 ] OPTIONAL ) ) ).
    cl_demo_output=>write_data( result3 ).

    cl_demo_output=>display( ).

Description

The result result1 of the first table expression has the row type of the internal table itab. The specified row is not found, which means that the structure def specified after DEFAULT is returned instead.

The result result2 of the second table expression is produced from a chaining with the structure component selector and is a component with the type string. Here, only the corresponding component of the structure def is specified as the default value.

The result result3 of the third table expression again has the row type of the internal table itab. Further table expressions with default values are specified as the default value. In the case shown here, the table expression in the second default value finds a row and returns it. If no searches are successful, an initial row is returned due to the closing OPTIONAL.