cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Delete in loop statement.. delete internal table details based on logic

sap_beginner98
Explorer
0 Likes
598

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.

Accepted Solutions (1)

Accepted Solutions (1)

MateuszAdamus
Active Contributor

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,
Mateusz
sap_beginner98
Explorer
0 Likes

Hi mateusz,

Thanks for your response.

line item 100,101,102 is an example . It will be varying for every object id like(201,301,401,etc)

In that case, How can I modify this code? and Is this code will not affect performance even when the records have values more than 50000?

MateuszAdamus
Active Contributor
0 Likes

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.

Kind regards,
Mateusz

Answers (0)