Search code examples
loopsabapinternal-tables

How to update internal table without using MODIFY?


I have created internal tables where I want to update age of employee in one internal table by calculating it from another table, I have done arithmetic calculations to get age but now how can I update it by any alternate way instead of MODIFY?

WRITE : / 'FirstName','LastName', '  Age'.  
LOOP AT gt_items1 INTO gwa_items1.
  READ TABLE gt_header INTO gwa_header WITH KEY empid = gwa_items1-empid.
  gwa_items1-age = gv_date+0(4) - gwa_header-bdate+0(4).
        
  MODIFY gt_items1 from gwa_items1 TRANSPORTING age WHERE empid = gwa_items1-empid.
  WRITE : /  gwa_items1-fname , gwa_items1-lname , gwa_items1-age .
ENDLOOP.

Solution

  • Use field symbols (instead of work areas) by LOOPing over internal tables:

    WRITE : / 'FirstName','LastName', '  Age'.  
    LOOP AT gt_items1 
         ASSIGNING FIELD-SYMBOL(<ls_item1>).
      READ TABLE gt_header 
           ASSIGNING FIELD-SYMBOL(<ls_header>) 
           WITH KEY empid = <ls_item1>-empid.
      IF sy-subrc EQ 0.
        <ls_item1>-age = gv_date+0(4) - <ls_header>-bdate+0(4).
        WRITE : /  <ls_item1>-fname , <ls_item1>-lname , <ls_item1>-age .
      ENDIF.
    ENDLOOP.
    

    Field symbols have two advantages:

    • They modify the internal table directly, no separate MODIFY is necessary.

    • They are somewhat faster, than work areas.