Application Development 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: 

add fields from tab2 to tab1

Former Member
0 Kudos

hallow

i have two internal tables with same type fields and i wont to add fields of table 2

to table 1 what is the best way to do that?

<b>i reward</b>

ex.

itab1

a1 a2 a3 a4 a5

1 2 3 4 5

itab 2

b1 b2 b3 b4 b5

6 7 8 9 10

i wont in itab 1

<b>a1 a2 a3 a4 a5

7 9 11 13 15 <---a1+b1 in field a1 in itab 1</b>

Regards

1 ACCEPTED SOLUTION

former_member223537
Active Contributor
0 Kudos

loop at itab1.

read table itab2 index sy-tabix.

itab1-a1 = itab1-a1 + itab2-b1.

itab1-a2 = itab1-a2 + itab2-b2.

itab1-a3 = itab1-a3 + itab2-b3.

itab1-a4 = itab1-a4 + itab2-b4.

itab1-a5 = itab1-a5 + itab2-b5.

modify itab1.

endloop.

8 REPLIES 8

kiran_k8
Active Contributor
0 Kudos

Antonio,

loop at the first internal table

read the second internal table with common fields

itab1-a1 = itab1-a1 + itab2-b1.

...

...

modify itab1.

endloop.

clear itab1

clear itab2.

K.Kiran.

0 Kudos

Hi,

Use like this


DATA: itab1_wa LIKE LINE OF itab1.
DATA: itab2_wa LIKE LINE OF itab2.
LOOP AT itab2 into itab2_wa.
itab1_wa = itab2_wa. "Since the types are same length will be same so you can do this
COLLECT itab1_wa INTO itab1.
ENDLOOP.

Regards,

Sesh

former_member223537
Active Contributor
0 Kudos

loop at itab1.

read table itab2 index sy-tabix.

itab1-a1 = itab1-a1 + itab2-b1.

itab1-a2 = itab1-a2 + itab2-b2.

itab1-a3 = itab1-a3 + itab2-b3.

itab1-a4 = itab1-a4 + itab2-b4.

itab1-a5 = itab1-a5 + itab2-b5.

modify itab1.

endloop.

Former Member
0 Kudos

Hi

loop at itab1.

index = sy-tabix.

loop at itab2 index index.

itab3-c1 = itab1-a1 + itab2-b1.

.

.

.

.

endloop.

append itab3.

clear itab2,itab1.

endloop.

reward points to all helpful answers

kiran.M

Former Member
0 Kudos

hi

use ADD-CORRESPONDING statement

if helpful, reward

Sathish. R

alejandro_bindi
Active Contributor
0 Kudos

This should work:


LOOP AT itab1 INTO wa1.
   READ TABLE itab2 INDEX sy-tabix INTO wa2.
   wa1 = wa2.
   COLLECT wa1 INTO itab1.
ENDLOOP.

varma_narayana
Active Contributor
0 Kudos

Hi..

<b>Try this ...</b> This can be better bcoz we are using the FIELD SYMBOLS so it will calucate the Sum for all the fields irrespective of how many are there.

Note: ADD-CORRESPONDING can be used only if the Fieldnames are same.

Field-symbols: <FS1> LIKE LINE OF ITAB1.

Field-symbols: <FS2> LIKE LINE OF ITAB1.

Field-symbols: <FS_FIELD1> TYPE ANY.

Field-symbols: <FS_FIELD2> TYPE ANY.

LOOP AT ITAB1 ASSIGNING <FS1>.

V_TABIX = SY-TABIX.

READ TABLE ITAB2 assigning <FS2> INDEX V_TABIX.

DO .

ASSIGN-COMPONENT SY-INDEX OF <FS2> TO <FS_FIELD2>.

IF SY-SUBRC NE 0.

EXIT.

ENDIF.

ASSIGN-COMPONENT SY-INDEX OF <FS1> TO <FS_FIELD1>.

IF SY-SUBRC NE 0.

EXIT.

ENDIF.

ADD <FS_FIELD2> <TO <FS_FIELD1>.

ENDDO.

ENDLOOP.

<b>Reward if Helpful</b>

Former Member
0 Kudos

HI,

do like this.

DESCRIBE TABLE itab1 lines sy-tfill.

do sy-tfill times.

READ TABLE itab1 index 1 into wa1.

READ TABLE itab2 index sy-index into wa2.

DELETE itab1 index 1.

wa1-a1 = wa1-a1 + wa2-b1.

wa1-a2 = wa1-a2 + wa2-b2.

wa1-a3 = wa1-a3 + wa2-b3.

wa1-a4 = wa1-a4 + wa2-b4.

wa1-a5 = wa1-a5 + wa2-b5.

APPEND wa1 to itab1.

enddo.

rgds,

bharat.