Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Processing Internal Data →  Internal Tables →  Expressions and Functions for Internal Tables →  FILTER - Filter Operator →  Examples of Table Filtering 

Table Filtering, Single Values

This example demonstrates table filtering using single values.

Other versions: 7.31 | 7.40 | 7.54

Source Code

    DATA carrid TYPE spfli-carrid VALUE 'LH'.
    cl_demo_input=>add_field( CHANGING field = carrid ).
    DATA cityfrom TYPE spfli-cityfrom VALUE 'Frankfurt'.
    cl_demo_input=>request( CHANGING field = cityfrom ).

    DATA spfli_tab TYPE STANDARD TABLE OF spfli
                   WITH EMPTY KEY
                   WITH NON-UNIQUE SORTED KEY carr_city
                        COMPONENTS carrid cityfrom.

    SELECT *
           FROM spfli
           INTO TABLE @spfli_tab.

    DATA(extract) =
      FILTER #( spfli_tab USING KEY carr_city
                  WHERE carrid   = CONV #( to_upper( carrid ) ) AND
                        cityfrom = CONV #( to_upper( cityfrom ) ) ).

    cl_demo_output=>display( extract ).

    DATA(rest) =
      FILTER #( spfli_tab EXCEPT USING KEY carr_city
                  WHERE carrid   = CONV #( to_upper( carrid ) ) AND
                        cityfrom = CONV #( to_upper( cityfrom ) ) ).

    ASSERT lines( extract ) + lines( rest ) = lines( spfli_tab ).

Description

Those rows are filtered from an internal table spfli_tab filled with flight data that have a specific value in the columns carrid and cityfrom and the result is assigned to an internal table extract. A suitable secondary key carr_city is defined for the internal table here.

A further internal table, rest, is given the same table filtering, but the addition EXCEPT is specified. The assertion ASSERT demonstrates that rest contains all rows from spfli_tab that are not in extract.