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.

Update internal table using the ABAP MODIFY statement command

*&-----------------------------------------------------------*
*& Report  ZMODIFYITAB                                       *
*&-----------------------------------------------------------*
*& Example of Modifying an internal table value              *
*&-----------------------------------------------------------*
*&-Created By details----------------------------------------*
*& Author : www.SAP Development                                 *
*&  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 assigning instead of the MODIFY

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 and modify 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 structure

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.


Make changes to 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 us8ng the MODIFY statement.

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


Syntax options for MODIFY statement

See details and syntax for all variations of the SAP MODIFY statement including modify itab, line, dbase, screen, source, target etc.

Related Articles

ABAP COLLECT statement syntax to add up all numeric internal table values within SAP
ABAP DELETE statement keyword to delete data from SAP internal and database tables
ABAP DESCRIBE statement keyword to get information about tables and fields
PERFORM TABLES command passing internal table as parameter
ABAP read command to read line of internal table in SAP
ABAP UPDATE command to modify database field values
AUTHORITY-CHECK abap Statement / command
ABAP delete command in SAP
ABAP WRITE statement command with in SAP to display data report to users
SAP ABAP Statement syntax including basic implementation code
Concatenate ABAP statement syntax for concatenating data objects, sting values or rows of an SAP internal table
ABAP EXPORT data TO MEMORY ID and import it back again
IF, CHECK & WHEN ABAP commends example source code and information
Call Function module IN BACKGROUND TASK - Execute abap code in seperate background process
Function module IN UPDATE TASK statement - Execute abap code in seperate unit of work
ABAP STRLEN command to get the value length of a SAP field
SAP ABAP SELECT command and its different uses
SELECT..ENDSELECT command
ABAP FOR ALL ENTRIES SELECT statement addition in SAP data retrieval
ABAP SELECT inner join statement to select from two tables at the same time
SELECT directly into an internal table
SELECT directly into an internal table when field order is different
Function module STARTING NEW TASK statement - Execute abap code in seperate work process