Application Development 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: 

Delete a row in FOR statement ABAP7.5

Rashmits
Explorer
1,362

Hi,

What would be the equivalent ABAP 7.5 code for below old code, basically deleting a row with blank level_2_bom value, if there is a row with level_2_bom value for combination of key fields.

LOOP AT gt_data INTO ls_data WHERE level_2_bom IS NOT INITIAL.
READ TABLE gt_Data INTO DATA(ls_data1) WITH KEY matnr = ls_data-matnr
swerk = ls_data-swerk
stlnr = ls_data-stlnr
level_1_bom = ls_data-level_1_bom
level_2_bom = ''.
IF sy-subrc = 0.
DELETE table gt_Data from ls_data1 .
ENDIF.
ENDLOOP.

8 REPLIES 8

Sandra_Rossi
Active Contributor
1,242

What did you try? If you know a little bit, you probably have the beginning code.

NB: the old code is missing lot of optimization, if you don't see that, I fear that going to a constructor expression won't improve. Code improvement is more important than using a constructor expression.

Rashmits
Explorer
0 Kudos
1,242

sandra.rossi could you elaborate

gt_Data is holding data from below table, BOMsubcomp = '' to be deleted(highlighted), if there is a BOMSubcomp value for the same combination of first 4 columns.

Sandra_Rossi
Active Contributor
0 Kudos
1,242

3 different accesses on GT_DATA (LOOP AT, READ TABLE, DELETE). How are keys defined?

I would be more confident if you had WITH TABLE KEY and DELETE TABLE.

Your turn now, to answer: what did you try?

Rashmits
Explorer
0 Kudos
1,242

Okay, so the internal table are the values from CDS view, and the condition in read statement are key fields. and delete is based on the local structure.
Now looking for some feedback and inputs from the expert 🙂

Sandra_Rossi
Active Contributor
0 Kudos
1,242

As you don't say what constructor expression you tried, I guess you don't know anything about the topic. I advise reading the documentation and trying all demo programs. After that, we can discuss.

Rashmits
Explorer
0 Kudos
1,242

i have tried the below rather to collect records than deleting.

Please feel free to not reply if you don't know the answer.

Sandra_Rossi
Active Contributor
0 Kudos
1,242

Please post the code as text instead of image, so that one can easily answer by copying your code in order to complete it. Then, select your code and press the button [CODE], which makes the code appear colored/indented, it will be easier for people to look at it. Thank you!

Sandra_Rossi
Active Contributor
0 Kudos
1,242

As you know, a Constructor Expression builds an internal table from scratch, Update or Delete are out of scope for constructor expressions.

Update and Delete are not good candidates for replacement by a constructor expression, due to performance and intermediate memory used.

If you still want to do it for some reason, you may filter the lines you copy via either the constructor operator FILTER or via the WHERE word.

But in your case, you use a kind of LINE_EXISTS (see ABAP doc if you don't know), so the constructor expression has to be complexified with a trick based on "LINES OF COND" to add either 0 or 1 line.

Also, using an internal table both as input and output is a classic problem, because a target variable is usually cleared at the start of the constructor expression so it requires to use another trick based on LET:

itab = VALUE #(
       LET temp_itab = itab IN
       FOR ... IN temp_itab
       WHERE ( ... )
       ( LINES OF COND ty_lines(
            WHEN line_exists( temp_itab[ ... ] ) THEN 
VALUE ty_line( ... ) ) ) ).

As you see, it's both less legible and less performing, so I advise to keep your current code.