Using FILTER with internal tables ABAP 7.4


Working with FILTER in internal tables ABAP 7.4, using filter and EXCEPT filter to filter internal table contents

A+ A-

In NetWeaver 7.4, SAP introduced new operator FILTER, which is very useful in filtering internal tables, prerequisite for FILTER is the filter table must have a sorted or hashed key, we use it in where clause.

Example using FILTER ABAP 7.4

In the below example we will using FILTER to filter(include filter) IT_MARA internal table for all 'FERT' records.

TYPES: BEGIN OF ty_filter,
      MTART TYPE MARA-MTART,
      END OF ty_filter.
DATA: IT_FILTER TYPE HASHED TABLE OF ty_filter WITH UNIQUE KEY MTART .
***INitialize filter Table
it_filter = VALUE #( ( MTART = 'FERT' ) ).

SELECT MATNR, MTART, MATKL
  FROM MARA
  INTO TABLE @DATA(it_mara)
  UP TO 1000 ROWS .

DATA(it_FERT) = FILTER #( it_mara in it_filter WHERE mtart = mtart  ).

LOOP AT it_fert INTO DATA(wa_fert).

  WRITE:/ wa_fert-matnr, wa_fert-mtart.

ENDLOOP.

Example using EXCEPT in FILTER ABAP 7.4

In the below example we will be filtering IT_MARA internal table using EXCEPT to exclude all 'FERT' materials, this work exactly opposite to FILTER.

TYPES: BEGIN OF ty_filter,
      MTART TYPE MARA-MTART,
      END OF ty_filter.
DATA: IT_FILTER TYPE HASHED TABLE OF ty_filter WITH UNIQUE KEY MTART .
***INitialize filter Table
it_filter = VALUE #( ( MTART = 'FERT' ) ).

SELECT MATNR, MTART, MATKL
  FROM MARA
  INTO TABLE @DATA(it_mara)
  UP TO 1000 ROWS .

DATA(it_FERT) = FILTER #( it_mara EXCEPT IN it_filter  WHERE mtart = mtart   ).

LOOP AT it_fert INTO DATA(wa_fert).
  WRITE:/ wa_fert-matnr, wa_fert-mtart.
ENDLOOP.

Learner Questions

No Questions by learners, be first one to ask ..!!

    Please Sign in to ask a question


    Was this lesson helpful to you? Yes No 1 People out of 1 think this lesson helpful

    Lesson Navigation

    ←Previous Chapter
    Next Lesson → Conditional Statements ABAP 7.4