Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Processing Internal Data →  Internal Tables →  Expressions and Functions for Internal Tables 

FILTER - Filter Operator

Other versions: 7.31 | 7.40 | 7.54

Syntax


... FILTER type( itab [EXCEPT] [IN ftab] [USING KEY keyname] 
                       WHERE c1 op f1 [AND c1 op f2 [...]] ) ...

Variants

1. ... FILTER type( itab ... )

2. ... FILTER type( itab ... IN ftab ... )

Extras

1. ... EXCEPT
2. ... USING KEY keyname
3. ... WHERE c1 op v1 [AND c1 op v2 [...]]

Effect

A constructor expression with the component operator FILTER creates a result of a table type specified using type. The rows are taken from an existing internal table itab in accordance with the condition after WHERE, converted to the row type of type, and inserted into the target table in accordance with the rules of INSERT ... INTO TABLE. In the first variant, the condition is constructed using single values and in the second variant using values from a filter table ftab.

The following can be specified for type:

  • The # character as a symbol for the operand type. If the data type required in an operand position is not unique and no fully identifiable, the type of itab is used (if identifiable).

itab is a functional operand position. The row type of itab must be convertible to the row type of the target type type.

The addition EXCEPT is used to read the rows that do not meet the WHERE condition. USING KEY can (or must) be used to specify a table key for evaluating itab or ftab (depending on the variant).


Notes

  • A table filter constructs the result row by row. If the result contains almost all rows in the source table, this method can be slower than copying the source table and deleting the surplus rows from the target table.

Variant 1

... FILTER type( itab ... )

Effect

Filtering using single values. The columns of a table key of itab are compared with single values in the WHERE condition. Those rows of itab are used that meet the WHERE condition or do not meet it when EXCEPT is specified.

Here, the internal table itab must have at least one sorted key or one hash key used for access. This can be

  • either the primary table key used without specifying USING KEY or by specifying its name primary_key after USING KEY,

This variant of a filter operator is not possible for an internal table itab without sorted key or hash key.


Variant 2

... FILTER type( itab ... IN ftab ...)

Effect

Filtering using values from an internal table ftab. In the WHERE condition, the columns of itab are compared with the values of the columns of the table key of the rows of the filter table ftab. Those rows in itab are used for which at least one row in ftab meets the WHERE condition or for which there is no row in ftab when EXCEPT is specified.

Here, the filter table ftab must have at least one sorted key or one hash key used for access. This can be

  • either the primary table key used without specifying USING KEY or by specifying its name primary_key after USING KEY,

This variant of a filter operator is not possible for an internal table ftab without sorted key or hash key. This variant does not place any requirements on the table keys of itab. ftab is a functional operand position.


Note

The row types of itab and ftab do not need to be identical.

Examples

Addition 1

... EXCEPT

Effect

If EXCEPT is not specified, those rows from itab are used that meet the WHERE condition. If EXCEPT is specified, those rows from itab are used that do not meet the WHERE condition.


Note

The addition EXCEPT is not the same as a negation of the WHERE condition, particularly in the second variant.

Addition 2

... USING KEY keyname

Effect

Table key keyname specified with which the WHERE condition is evaluated.

  • In the first variant, a sorted key or a hash key of the table itab can be specified. If the primary key of itab is not a sorted key or hash key, itab must have a secondary key of this type and it must be specified using USING KEY.
  • In the second variant, a sorted key or a hash key of the filter table ftab can be specified. If the primary key of ftab is not a sorted key or hash key, ftab must have a secondary key of this type and it must be specified using USING KEY.

Addition 3

... WHERE c1 op f1 [AND c1 op f2 [...]]

Effect

A condition for the table key used in the FILTER expression must be specified after WHERE:

Multiple comparisons can be joined using AND only. There can be no further comparisons alongside those mentioned for key components.

  • In the first variant, key components of the internal table itab must be specified for the left operands c1, c2, ... On the right side, data objects f1, f2, ... must be specified that are compatible with the left side. f1, f2, ... are general expression positions.
  • In the second variant, key components of the filter table ftab must be specified for the right operands f1, f2, ... On the left side, components of the internal table itab must be specified that are compatible with the right side.


Notes

  • The boolean operators NOT, OR, and EQUIV cannot be used in the WHERE condition.

Continue

Examples of Table Filtering