Share |

ABAP MODIFY command to modify database tables and internal tables


Change an internal table using the MODIFY command
Changing values within an internal table using the MODIFY command is a very powerfull yet simple process to perform. The code below demonstrates how you can populate an internal table and then change the value of a specific field within that internal table.

*&-----------------------------------------------------------*
*& 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_ekko,
  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_ekko.

DATA: it_ekko TYPE STANDARD TABLE OF t_ekko INITIAL SIZE 0,
      wa_ekko TYPE t_ekko.

************************************************************************
*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_ekko.

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

  loop at it_ekko into wa_ekko.
    wa_ekko-netpr = '100'.
    MODIFY it_ekko INDEX sy-tabix FROM wa_ekko
         TRANSPORTING netpr.
  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_ekko into wa_ekko with key ebeln = ld_ebeln.
if sy-subrc eq 0.
  MODIFY it_ekko  from wa_ekko index sy-tabix.
else.
  APPEND wa_ekko to it_ekko.
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.