Comments

ABAP MODIFY statement to update SAP data within database and internal tables





Changing values within an internal table using the MODIFY statement is a very powerfull yet simple process to perform. The ABAP code snippets below demonstrate various differnet ways of using the ABAP MODIFY statement. First it populates an intenal table with data and then performs the following functionality on it using the MODIFY syntax:

UPDATE: inline declaration works from 7.4

Using field symbol
loop at it_ekpo assigning field-symbol(<fs1>).
<fs1>-netpr = '222'.
endloop.

Using work area
loop at it_ekpo into data(wa_ekpo4).
wa_ekpo4-netpr = '555'.
MODIFY it_ekpo from wa_ekpo4 index sy-tabix.
endloop.

Change an internal table using the ABAP MODIFY statement command

*&-----------------------------------------------------------*
*& Report  ZMODIFYITAB                                       *
*&-----------------------------------------------------------*
*& Example of Modifying an internal table value              *
*&-----------------------------------------------------------*
*&-Created By details----------------------------------------*
*& Author : www.sapdev.co.uk                                 *
*&  SAP ABAP development                                    *
*&-----------------------------------------------------------*
Report  ZMODIFYITAB.

type-pools: slis.                                 "ALV Declarations
*Data Declaration
*----------------
TYPES: BEGIN OF t_ekpo,
  ebeln TYPE ekpo-ebeln,
  ebelp TYPE ekpo-ebelp,
  statu TYPE ekpo-statu,
  aedat TYPE ekpo-aedat,
  matnr TYPE ekpo-matnr,
  menge TYPE ekpo-menge,
  meins TYPE ekpo-meins,
  netpr TYPE ekpo-netpr,
  peinh TYPE ekpo-peinh,
 END OF t_ekpo.

DATA: it_ekpo TYPE STANDARD TABLE OF t_ekpo INITIAL SIZE 0,
      wa_ekpo TYPE t_ekpo,
      it_ekpo2 TYPE STANDARD TABLE OF t_ekpo INITIAL SIZE 0 WITH HEADER LINE.

DATA: gd_tabix type sy-tabix.


**************************************************************
*Start-of-selection.
START-OF-SELECTION.

  select ebeln ebelp statu aedat matnr menge meins netpr peinh
   up to 10 rows
    from ekpo
    into table it_ekpo.

**************************************************************
*End-of-selection.
END-OF-SELECTION.

* Modify data within internal table loop using the index and TRANSPORTING additions
  loop at it_ekpo into wa_ekpo.
    gd_tabix = sy-tabix.
    wa_ekpo-netpr = '100'.
    MODIFY it_ekpo INDEX gd_tabix FROM wa_ekpo
         TRANSPORTING netpr.
  endloop.

* This example could also look like this...But if you are only updating one field
* the above code helps performance by specifying that field
  loop at it_ekpo into wa_ekpo.
    gd_tabix = sy-tabix.
    wa_ekpo-netpr = '100'.
    MODIFY it_ekpo INDEX gd_tabix FROM wa_ekpo.
  endloop.

* If you wanted to go even more old school and had a itab with a
* header line you could even reduce the code to
  loop at it_ekpo2.
    gd_tabix = sy-tabix.
    it_ekpo2-netpr = '100'.
    MODIFY it_ekpo2 INDEX gd_tabix.
  endloop.
 

Do i need the "INDEX gd_tabix" for all the above examples??

The simple answer is no and the following code should work just fine!!
  loop at it_ekpo into wa_ekpo.
    wa_ekpo-netpr = '100'.
    MODIFY it_ekpo FROM wa_ekpo TRANSPORTING netpr.
* or MODIFY it_ekpo FROM wa_ekpo.
  endloop.

....But and the only reason I say "should" is because on many occasions the above code has not worked as expected and has not updated the data I expected it too... This may have been for many reasons including me having a bit of a dumb moment or something else all together causing the issue but I now always include the index value if using MODIFY. I also dont rely on MODIFY to ADD new rows either. i.e. I check if the record exists and if not I use the APPEND statement, see code below.

Should you use a FIELD-SYMBOL instead of the MODIFY statement

For this specific task (i.e. looping around an internal table and updating values), especially if you take into account the potential issue highlighted above you probably should just use a field symbol instead of the MODIFY command. But I know some people dont really like using these, unless it is for something that can only be done effectively with a field symbol. Just for info this is how the ABAP code would look if you replicated the MODIFY functionality using a FIELD-SYMBOL:
FIELD-SYMBOLS: <fs_ekpo> LIKE LINE OF it_ekpo.
LOOP AT it_ekpo ASSIGNING .
  -netpr = '444'.
ENDLOOP.

Add/Change a row in internal table

You would not use the modify command to add an entry to an internal table, this would be done using the APPEND command. When updating an itab simply read the table first to check if an entry exists with required key and then MODIFY or APPEND based on the result:
READ it_ekpo into wa_ekpo with key ebeln = ld_ebeln.
if sy-subrc eq 0.
  MODIFY it_ekpo  from wa_ekpo index sy-tabix.
else.
  APPEND wa_ekpo to it_ekpo.
endif.

MODIFY a database table from a work area

The below abap code demonstrates how to add or update a SAP database table row with the contents of a work area (structure).

* Updates database table Zdtable with the contents of wa_itab
MODIFY Zdtable FROM wa_itab.

If you only want to update a single field within a database table you might want to use the UPDATE command instead.


MODIFY a database table from an internal table

The below abap code shows how to add or update a SAP database table row with the contents of a internal table.

* Updates database table Zdtable with the contents of it_itab
MODIFY Zdtable FROM TABLE it_itab.




sapdev logo background
sapdev logo sapdev logo


Can't find something on ERPWorkbench? A quick search should fix that: