sumedh patil
sumedh patil

Reputation: 59

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.

Upvotes: 0

Views: 15663

Answers (2)

Nelson Miranda
Nelson Miranda

Reputation: 5564

Besides József Szikszai's answer you could also use references:

write : / 'FirstName','LastName', '  Age'.

sort gt_header by empid. " <------------- Sort for binary search

loop at gt_items1 reference into data(r_item1).

  read table gt_header reference into data(r_header)
       with key empid = r_item1->empid binary search. " <------------- Faster read

  check sy-subrc eq 0. 

  r_item1->age = gv_date+0(4) - r_header->bdate+0(4).
  write : /  r_item1->fname , r_item1->lname , r_item1->age .
endloop.

I added some enhacements to your code also.

For more info check this link.

Upvotes: 1

J&#243;zsef Szikszai
J&#243;zsef Szikszai

Reputation: 5071

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.

Upvotes: 6

Related Questions