Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Processing Internal Data →  Internal Tables →  Expressions and Functions for Internal Tables →  FILTER - Filter Operator 

FILTER - Filter Table

Other versions: 7.31 | 7.40 | 7.54

Syntax


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

Extras

1. ... USING KEY keyname

2. ... WHERE c1 op v1 [AND c2 op v2 [...]]

Effect

This variant of the filter operator FILTER filters itab 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. ftab is a functional operand position.

Depending on its position, USING KEY can be used to specify either a key of the table ftab or the table itab, which is then used to access the table. If USING KEY is not used, the filter table ftab must have a sorted key or a hash key as the primary table key, which is then used to access the filter table.


Note

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


Example

Filters three rows of the internal table carriers. The filter table has a sorted primary key. This is used implicitly and the addition USING KEY does not have to be specified.

SELECT * 
       FROM scarr 
       INTO TABLE @DATA(carriers). 

DATA filter TYPE SORTED TABLE OF scarr-carrid 
                 WITH UNIQUE KEY table_line. 
filter = VALUE #( ( 'AA ' ) ( 'LH ' ) ( 'UA ' ) ). 

cl_demo_output=>display( FILTER #( 
  carriers IN filter WHERE carrid = table_line ) ). 

Addition 1

... USING KEY keyname

Effect

Table key keyname specified with which the WHERE condition is evaluated. A sorted key or a hash key of the filter table (after ftab) or of the source table (after itab) can be specified. This can be the primary table key or a secondary table key that is specified using primary_key or the corresponding name.

  • When USING KEY is specified for the filter table ftab, it must have a sorted key or a hash key. There are no requirements on the table keys of itab. The specified key is used to access the filter table.
  • When USING KEY is specified for the source table itab, it must have a sorted key or a hash key. There are no requirements on the table keys of ftab. The specified key is used to access the source table.
  • If USING KEY is not specified for ftab or itab, the filter table ftab must be a sorted table or a hash table and the primary table key is used implicitly when accessing the filter table. There are no requirements on the table keys of itab.


Note

Which tables a key is specified for depends on the optimization it is intended to bring about.


Example

Like the preceding example, but here the addition USING KEY must be used for one of the tables, since the internal table filter is a standard table without a primary key.

SELECT * 
       FROM scarr 
       INTO TABLE @DATA(carriers). 

DATA filter TYPE STANDARD TABLE OF scarr-carrid 
                 WITH EMPTY KEY 
                 WITH UNIQUE SORTED KEY line COMPONENTS table_line. 
filter = VALUE #( ( 'AA ' ) ( 'LH ' ) ( 'UA ' ) ). 

cl_demo_output=>display( FILTER #( 
  carriers IN filter USING KEY line 
           WHERE carrid = table_line ) ).

Example

Like the preceding example, but here the addition USING KEY is used for the other table.

DATA carriers TYPE SORTED TABLE OF scarr 
              WITH UNIQUE KEY carrid. 
SELECT * 
       FROM scarr 
       INTO TABLE carriers. 

DATA filter TYPE STANDARD TABLE OF scarr-carrid 
                 WITH EMPTY KEY. 
filter = VALUE #( ( 'AA ' ) ( 'LH ' ) ( 'UA ' ) ). 

cl_demo_output=>display( FILTER #( 
  carriers USING KEY primary_key IN filter 
           WHERE carrid = table_line ) ). 

Addition 2

... WHERE c1 op f1 [AND c2 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 variant with the filter table, key components of the filter table ftab must be specified for the right operands f1, f2, and so on. 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.

Executable Examples