Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Logic in internal table

Former Member
0 Likes
677

Hi,

I got a requirement where i am reading some date into 2 internal table say i_1 and i_2.Then i need to loop in i_2 and check if there is a record with the same key fields in table i_1 if so add the value of the key figures from i_2 to i_1.Finally delete all entries from i_2.

can anyone tell me a logic for this.

Regards,

Ravi

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
633

Hi,

Try this



sort it_tab2 by field1 field2.   "Assuming field1 and field2 are key fields in it_tab2
sort it_tab1 by field1 field2.   "So sort this table with field1 and field2
loop at it_tab2 into wa_tab2.
  lv_tabix = sy-tabix.
  read table it_tab1 into wa_tab1 with key field1 = it_tab1-field1
                                                                   field2 = it_tab2-field2 binary search.  "Also assuming you have multiple records in it_tab1
  loop at it_tab1 into wa_tab1 from sy-tabix.
    wa_tab2-field1 = wa_itab2-field1 + wa_itab1-field1.
    wa_tab2-field2 = wa_itab2-field2 + wa_itab1-field2.
    modify it_tab2 from wa_tab2 index lv_tabix transporting field1 field2.
    delete it_tab1 from wa_tab1index sy-tabix. "Re-check this line. I m not sure of the syntax
  endloop.
endloop.

4 REPLIES 4
Read only

Former Member
0 Likes
633

Hello,

Just convert what ever you asked in to code and it should work. If you still face issues, post the code with what you have tried.

Vikranth

Read only

Former Member
0 Likes
634

Hi,

Try this



sort it_tab2 by field1 field2.   "Assuming field1 and field2 are key fields in it_tab2
sort it_tab1 by field1 field2.   "So sort this table with field1 and field2
loop at it_tab2 into wa_tab2.
  lv_tabix = sy-tabix.
  read table it_tab1 into wa_tab1 with key field1 = it_tab1-field1
                                                                   field2 = it_tab2-field2 binary search.  "Also assuming you have multiple records in it_tab1
  loop at it_tab1 into wa_tab1 from sy-tabix.
    wa_tab2-field1 = wa_itab2-field1 + wa_itab1-field1.
    wa_tab2-field2 = wa_itab2-field2 + wa_itab1-field2.
    modify it_tab2 from wa_tab2 index lv_tabix transporting field1 field2.
    delete it_tab1 from wa_tab1index sy-tabix. "Re-check this line. I m not sure of the syntax
  endloop.
endloop.

Read only

Former Member
0 Likes
633

Hi.

loop in i_2

use read stmt to read i_1 table and check with ur condition

if true...

move this entry

else delete the entry.

Read only

former_member404244
Active Contributor
0 Likes
633

hi,

Declare one more internal table i_3.



Loop at i_2.

read table i_1 with key field1 = i_2-field1.

if sy-subrc eq 0.

move the data of i_2  to other intarnal table say it is i_3.

append i_3.

delete i_2.
endif.
endloop.

Now i_3 will have your required data.

Regards,

Nagaraj