How to delete itab rows via range?

2.4k views Asked by At

I have a main internal table ITAB:

enter image description here

I have casted the field DATA_STRING (with a pre-defined structure):

enter image description here

I have a range with one of the rows from above that needs to be deleted:

enter image description here

Code snippet:

LOOP AT itab ASSIGNING FIELD-SYMBOL(<fs1>).
 ASSIGN <fs1>-data_string TO <fs2> CASTING.
 IF <fs2>-number IN range[].
   DELETE ITAB from <fs1>.
 ENDIF. 
ENDLOOP. 

This might be a dumb question but is there a way to access and delete the row from ITAB without going through the loop? Something like

DELETE itab where data_string-number IN range[].

Or is there a way to directly extract DATA_STRING field to ITAB2 (image 2) without a loop?

2

There are 2 answers

0
Suncatcher On

If the range is filled correctly, it should work like this:

DELETE itab WHERE number IN range.

Full working simple:

RANGES: lr_matkl  FOR t023-matkl.
DATA: lt_t023 TYPE TABLE OF t023.
DATA: lt_mara TYPE TABLE OF mara.

SELECT * UP TO 10 ROWS
  FROM t023
  INTO TABLE lt_t023.

SELECT *
  FROM mara
  INTO TABLE lt_mara.

LOOP AT lt_t023 ASSIGNING FIELD-SYMBOL(<fs_matkl>) WHERE matkl <> space.
  lr_matkl-sign   = 'I'.
  lr_matkl-option = 'EQ'.
  lr_matkl-low    = <fs_matkl>-matkl.
  APPEND lr_matkl.
ENDLOOP.

CHECK lr_matkl IS NOT INITIAL.
DELETE lt_mara WHERE matkl IN lr_matkl.
0
Sandra Rossi On

With a dynamic internal table (structure of lines unknown at compile time), you may use a dynamic condition with DELETE since at least ABAP 7.02, e.g.:

DELETE itab WHERE (`component = 'value'`).

Minimal reproducible example:

FIELD-SYMBOLS <table> TYPE ANY TABLE.
TYPES ty_airlines TYPE STANDARD TABLE OF scarr-carrid WITH EMPTY KEY.
TYPES ty_range_airlines TYPE RANGE OF scarr-carrid.

DATA(airlines) = VALUE ty_airlines( ).
ASSIGN airlines TO <table>.

airlines = VALUE #( ( 'AA' ) ( 'AF' ) ( 'LH' ) ).
DATA(range_airlines) = VALUE ty_range_airlines( ( sign = 'I' option = 'EQ' low = 'AF' ) ).

DELETE <table> WHERE ('table_line IN range_airlines').

ASSERT airlines = VALUE ty_airlines( ( 'AA' ) ( 'LH' ) ).

NB: the code is using table_line, but that's not related to the question (you may of course use component names of the internal table if the lines are structured, e.g. data_string-number in the case of the OP question).