SAP ECC 6.0 (Release 700, SP 12).
This article explains how to avoid modify and delete statements inside loop...end loop statements using field symbols.
Author(s): Rahul Babukuttan
Company: Applexus Technologies (P) Ltd.
Created on: 11 January 2012
Rahul Babukuttan, Applexus Technologies (P) Ltd.
B.Tech Computer Science Graduate, working as SAP Technology Consultant
Table of Contents
In order to modify/delete records of internal tables we use modify/delete inside loop...end loop statements in ABAP. This may lead to serious performance issues if the internal table we use is very large. We can simply move these modify/delete statements outside the loop..end loop statements thus optimizing the performance. We can achieve this by the use of field symbols.
How we can achieve this?
Step 1: We loop the internal table into field symbol, any assignment to the field symbol will modify the data directly without modify statement. Thus minimizing the use of "MODIFY" statement
Step 1: We need to modify the internal table type by adding one field named deletion indicator at the end of the type declaration.
Step 2: We check for specific condition inside the loop..end loop statement and the deletion indicator is set which satisfies our required condition
Step 3: Outside the loop..end loop, we write the delete statement by adding the condition DELETE itab WHERE deletion indicator eq 'X'.
Given below is the abap coding done to delete all records from internal table T_VBAP for all materials which is marked for deletion (LVORM field) in the MARA internal table T_MARA.
//This is a code sample block
TYPES: BEGIN OF ty_vbap, vbeln TYPE vbeln_va, posnr TYPE posnr_va, matnr TYPE matnr, kwmeng TYPE kwmeng, del_indicator TYPE char01, "Deletion marker END OF ty_vbap, BEGIN OF ty_mara, matnr TYPE matnr, lvorm TYPE lvorm, END OF ty_mara. DATA: t_vbap TYPE STANDARD TABLE OF ty_vbap, lt_vbap TYPE STANDARD TABLE OF ty_vbap, t_mara TYPE STANDARD TABLE OF ty_mara. FIELD-SYMBOLS: <fs_vbap> TYPE ty_vbap. SELECT vbeln posnr matnr kwmeng FROM vbap INTO TABLE t_vbap UP TO 3000 ROWS. IF t_vbap IS NOT INITIAL. lt_vbap = t_vbap. SORT lt_vbap BY matnr. DELETE ADJACENT DUPLICATES FROM lt_vbap COMPARING matnr. SELECT matnr lvorm FROM mara INTO TABLE t_mara FOR ALL ENTRIES IN lt_vbap WHERE matnr EQ lt_vbap-matnr. ENDIF. LOOP AT t_vbap ASSIGNING <fs_vbap>. IF <fs_vbap>-matnr CS 'abcd'. READ TABLE t_mara WITH KEY matnr = <fs_vbap>-matnr lvorm = 'X' TRANSPORTING NO FIELDS. IF sy-subrc EQ 0. <fs_vbap>-del_indicator = 'X'. "mark for deletion ENDIF. ELSE. <fs_vbap>-kwmeng = 20. "instead of modify statement we assign values directly to field symbols ENDIF. ENDLOOP. DELETE t_vbap WHERE del_indicator EQ 'X'.
In the code above, instead of using a modify statement inside the loop..end loop statement we assign the field symbol <fs_vbap> and make changes to the field of the field symbol which reflects to the values directly.
Note: Instead of putting the delete statement inside the loop..end loop statement, we mark the records which needs to be deleted based on the particular condition and do the deletion outside the loop.
Short quick summary to catch the reader's attention and of course the search engine