on ‎2020 Nov 30 12:33 PM
Hi All,
While using DELETE in loop statement, it will affect in performance. so I need to change this code. How can I change it for this code?

Logic: For an object id (lv_doc), It has lv_mod = 101 line item, then delete line item 100 from that object id.
If object id has line item 102, then delete line item 100,101 including 102 from that object id.
For this, Can someone suggest different logic if it can be improved technically?
Please suggest, How to delete without using DELETE statement inside the loop?
LOOP AT lt_finaloutput INTO wa_finaloutput.
lv_mod = wa_finaloutput-number_int.
lv_doc = wa_finaloutput-object_id.
READ TABLE lt_finaloutput INTO wa_finaloutput WITH KEY object_id = lv_doc number_int = lv_mod.
IF sy-subrc EQ 0.
IF lv_mod MOD 10 EQ 1.
lv_mod = lv_mod - 1.
DELETE lt_finaloutput WHERE object_id = lv_doc AND number_int = lv_mod.
ELSEIF lv_mod MOD 10 EQ 2.
DELETE lt_finaloutput WHERE object_id = lv_doc AND number_int = lv_mod.
lv_mod = lv_mod - 1.
DELETE lt_finaloutput WHERE object_id = lv_doc AND number_int = lv_mod.
lv_mod = lv_mod - 1.
DELETE lt_finaloutput WHERE object_id = lv_doc AND number_int = lv_mod.
ENDIF.
ENDIF.
ENDLOOP.
Request clarification before answering.
Hello sap_beginner98
SORT lt_finaloutput BY object_id number_int.
LOOP AT lt_finaloutput REFERENCE INTO DATA(ld_finaloutput).
AT NEW object_id.
READ TABLE lt_finaloutput TRANSPORTING NO FIELDS
WITH KEY object_id = ld_finaloutput->object_id
number_int = 102.
IF sy-subrc = 0.
DELETE lt_finaloutput
WHERE object_id = ld_finaloutput->object_id
AND number_int >= 100
AND number_int <= 102.
CONTINUE.
ENDIF.
READ TABLE lt_finaloutput TRANSPORTING NO FIELDS
WITH KEY object_id = ld_finaloutput->object_id
number_int = 101.
IF sy-subrc = 0.
DELETE lt_finaloutput
WHERE object_id = ld_finaloutput->object_id
AND number_int = 100.
CONTINUE.
ENDIF.
ENDAT.
ENDLOOP.Kind regards,You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I see. Let's try this again then.
TYPES:
BEGIN OF yls_record,
object_id TYPE vbeln,
number_int TYPE posnr,
delete TYPE flag,
END OF yls_records,
ylt_records TYPE SORTED TABLE OF yls_record WITH UNIQUE KEY object_id number_int.
DATA:
lt_records TYPE ylt_records.
INSERT LINES OF lt_finaloutput INTO TABLE lt_records.
LOOP AT lt_records REFERENCE INTO DATA(ld_record).
DATA(lv_tabix) = sy-tabix.
lv_mod = ld_record->number_int MOD 10.
CASE lv_mod.
WHEN 1.
ld_record->delete = abap_true.
" modify 100
DATA(lv_new_tabix) = lv_tabix - 1.
READ TABLE lt_records REFERENCE INTO DATA(ld_record_tmp) INDEX lv_new_tabix.
CHECK sy-subrc = 0.
ld_record_tmp->delete = abap_true.
WHEN 2.
ld_record->delete = abap_true.
WHEN OTHERS.
" do nothing.
ENDLOOP.
DELETE lt_records WHERE delete = abap_true.| User | Count |
|---|---|
| 4 | |
| 2 | |
| 1 | |
| 1 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.