Dear saplearners, in this blog we all are going to learn about abap internal table filtering using FILTER operator in ABAP 7.4
A new FILTER operator is available which can used on ABAP internal tables to filter the data (or) to retrieve subset of data into a new internal table. As of ABAP 7.4 this keyword is available to use in the system.
The result of FILTER operator is a new internal table with filtered data.
First of all, lets see how we can perform the internal table filtering in the system before ABAP 7.4 version. I am sure that we all end up in creating a LOOP with a WHERE condition and finally doing an APPEND to another internal table.
ABAP code snippet will look something like this
DATA: lt_flights_all TYPE STANDARD TABLE OF spfli, lt_flight_lh TYPE STANDARD TABLE OF spfli. SELECT * FROM spfli INTO TABLE @lt_flights_all. IF sy-subrc = 0. LOOP AT lt_flights_all INTO DATA(ls_flight) WHERE carrid = 'LH'. APPEND ls_flight TO lt_flight_lh. CLEAR: ls_flight. ENDLOOP. ENDIF.
that’s hell of a coding lines written for just simple task and if the complexity of filtering logic increase we need to write more lines of ABAP code. We definitely need some change here, is it ?
So, as of ABAP 7.4 , the FILTER keyword came into our world to save us from those extra keyboard hits. Lets let get to know more about it with some examples.
FILTER with single values
Lets take the same above example and refine the internal table filtering logic with the new FILTER keyword.
DATA: lt_flights_all TYPE STANDARD TABLE OF spfli WITH NON-UNIQUE SORTED KEY carrid COMPONENTS carrid, lt_flight_lh TYPE STANDARD TABLE OF spfli. SELECT * FROM spfli INTO TABLE @lt_flights_all. IF sy-subrc = 0. lt_flight_lh = FILTER #( lt_flights_all USING KEY carrid WHERE carrid = 'LH ' ). ENDIF.
as a result, the internal table LT_FLIGHTS_ALL is filtered on WHERE condition and filtered data will be available in the internal table LT_FLIGHT_LH.
Yes, that is just a line of statement to filter the data using FILTER keyword. Cool it is..!
The above example is a basic syntax form using FILTER keyword, now look at the another syntax form with filter table
FILTER with filter table
To explain FILTER with filter table syntax form, we need two internal tables. One which contains the actual data on which filtering is applied and other is filter internal table which contains the filter values used for filtering.
In previous example we performed the filter with a single value, now add some more filter values. Sample code snippet will look like something below.
DATA: lt_flights_all TYPE STANDARD TABLE OF spfli WITH NON-UNIQUE SORTED KEY carrid COMPONENTS carrid, lt_flight_final TYPE STANDARD TABLE OF spfli. SELECT * FROM spfli INTO TABLE @lt_flights_all. * Create a filter internal table with multiple values DATA filter_tab TYPE SORTED TABLE OF scarr-carrid WITH UNIQUE KEY table_line. filter_tab = VALUE #( ( 'AA ' ) ( 'LH ' ) ). * Apply filters lt_flight_final = FILTER #( lt_flights_all IN filter_tab WHERE carrid = table_line ). cl_demo_output=>write_data( lt_flights_all ). cl_demo_output=>write_data( lt_flight_final ). cl_demo_output=>display( ).
So by using the help filter table you can apply FILTERing on internal tables. Output look like below
Below are some points to keep in mind when using the FILTER operator
- The internal table on which FILTER operator is used must have at least one sorted key or one hash key used for access.
- The row type of main internal table and result internal table do not need to be identical
- The Boolean operators NOT, OR, and EQUIV cannot be used in the WHERE condition.
Congrats..! you have learned about FILTER operator, try this in you next ABAP program and let me know feedback. Please feel free to comment and let us know your feedback. Subscribe for more updates.
If you liked it, please share it! Thanks!