Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Delete internal table data with another internal table without loop

41,669

Hi,

We have data in one internal table IT_TAB1 and it has Entity, source system columns. Another internal table IT_TAB2 having source system column only.

How to delete the records in IT_TAB1 where source system not in IT_TAB2 without using LOOP?

Is is possible in advanced ABAP with different declarations ?

1 ACCEPTED SOLUTION
Read only

Sandra_Rossi
Active Contributor
30,208

Yes, for instance

it_tab1 = FILTER #( it_tab1 IN it_tab2 WHERE tab1_srcsystem = tab2_srcsystem ).

it_tab2 must be sorted or hashed.

4 REPLIES 4
Read only

Sandra_Rossi
Active Contributor
30,209

Yes, for instance

it_tab1 = FILTER #( it_tab1 IN it_tab2 WHERE tab1_srcsystem = tab2_srcsystem ).

it_tab2 must be sorted or hashed.

Read only

OlegBash
Active Participant
30,208

If the version if ABAP is one of the modern, you can try use operator FILTER with keyword EXCEPT

It is described in help

https://help.sap.com/doc/abapdocu_752_index_htm/7.52/en-us/abenconstructor_expr_filter_table.htm

Example could be like that (as a result in internal table lt_itab1 will be only lines that are NOT in lt_itab2)

    types: BEGIN OF ts_line1
              , src_sys TYPE sysysid
              , field1 TYPE char10
              , field2 TYPE char10
          , END OF ts_line1
          , tt_line1 TYPE STANDARD TABLE OF ts_line1
            with NON-UNIQUE SORTED KEY PR COMPONENTS src_sys
          .

    types: BEGIN OF ts_line2
              , src_sys TYPE sysysid
          , END OF ts_line2
          , tt_line2 TYPE STANDARD TABLE OF ts_line2
          .

    data lt_itab1 TYPE tt_line1.
    data lt_itab2 TYPE tt_line2.


    lt_itab1 = VALUE #(  ( src_sys = 'ONE' field1 = 'F11' field2 = 'F21' )
                                ( src_sys = 'TWO' field1 = 'F12' field2 = 'F22' )
                                ( src_sys = 'THREE' field1 = 'F13' field2 = 'F23' )
                                ( src_sys = 'FOUR' field1 = 'F14' field2 = 'F24' ) ).


    lt_itab2 = VALUE #( ( src_sys = 'TWO' ) ( src_sys = 'FOUR' ) ).

""" to delete records that are not in lt_itab2
data(lt_itab10) = FILTER #( lt_itab1 USING key pr IN lt_itab2  WHERE src_sys = src_sys ).


""" to save records that are not in lt_itab2
    data(lt_itab11) = FILTER #(
       lt_itab1 USING KEY pr EXCEPT IN lt_itab2
       where src_sys = src_sys
    ).
" lt_itab11  will store ONE / THREE but not TWO and FOUR
Read only

0 Likes
30,208

Thank you olegbash599 for your detailed explanation. I have tried the Sandra approach and it works. Can you explain me "withNON-UNIQUESORTEDKEY PR COMPONENTS src_sys" this logic and how it filtered at the end please. Also, help me to understand that how your approch is different from Sandra approach.

Read only

OlegBash
Active Participant
0 Likes
30,208

praveen_channa
The differences of the approaches are:
1) With keyword EXCEPT the system could filter the lines which is in the second table. So it is comparing not by equality but inequality.
In your question I do have noticed this phrase "delete the records in IT_TAB1 where source system not in IT_TAB2".
So Sandra's approach is more applicable in that case.

2) using ley allows you to avoid using sorted type for example. It is what I was intend to show (how we can use standard table with FILTER function).

Thank you for your valuable question