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

append internal tables

Former Member
0 Likes
2,497

Hi all,

I had two internal tables like itab and itab1.

itab:

belnr matnr

100001 230000

100002 230001

and itab1 :

belnr hwbas.

100001 2387.90

100002 980.00

i want to append both internal table in 3rd one like itab2.

itab2 :

belnr matnr hwbas

100001 230000 2387.90

100002 230001 980.00

How to do this..

Please help me..........

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,467

" Add MATNR Field in also itab1. Then change that line of code
Loop at itab.
itab2-belnr = itab-belnr.
itab2-matnr = itab-matnr.
Read table itab1 with key belnr = itab-belnr matnr = itab-matnr. " change this line of code
If sy-subrc = 0.
itab2-hwbas = itab1-hwbas.
endif.
append itab2.
clear itab2.
clear itab1.
clear itab.
endloop.

Edited by: tahir naqqash on Feb 24, 2009 4:25 PM

24 REPLIES 24
Read only

GauthamV
Active Contributor
0 Likes
2,467

Loop at itab.
itab2-belnr = itab-belnr.
itab2-matnr = itab-matnr.
Read table itab1 with key belnr = itab-belnr.
If sy-subrc = 0.
itab2-hwbas = itab1-hwbas.
endif.
append itab2.
clear itab2.
clear itab1.
clear itab.
endloop.
Read only

Former Member
0 Likes
2,467

Hi Gautham...

after using your code i get copied value for hwbas field ...

output is like:

belnr matnr hwbas

100001 230000 8978.00

100002 230001 8978.00

I WANT LIKE :

belnr matnr hwbas

100001 230000 8978.00

100002 230001 980.00

pLEASE HELP ME ..

Read only

GauthamV
Active Contributor
0 Likes
2,467

Hello Ankita,

The logic i had mentioned should work.

Check in debugging mode whether itab1 is clearwed in the loop

or else use clear itab1 before read statement.

Read only

Former Member
0 Likes
2,467

Hi gautham,

There is till problem..

if belnr is same for two line item then it takes same hwbas...

like:

belnr matnr hwbas

100000 2300001 9890.0

100000 2300002 9890.0

i want like :

belnr matnr hwbas

100000 2300001 9890.0

100000 2300002 790.0

Please help me...

Read only

0 Likes
2,467

Hi, Ankita

In fact you must have the the Unique Records for this in both of Internal Tables Please Refer to following Sample Code. Hope will help you.

DATA: BEGIN OF it1 OCCURS 10,
  key1(10),
  key2(10),
  a TYPE i,
END OF it1.

DATA: BEGIN OF it2 OCCURS 10,
  key1(10),
  key2(10),
  b TYPE i,
 END OF it2.
DATA: BEGIN OF it3 OCCURS 10,
 key1(10),
 key2(10),
 a TYPE i,
 b TYPE i,
END OF it3.

it1-key1 = '10001'.
it1-key2 = '20002'.
it1-a = 250.
APPEND it1 TO it1.
it1-key1 = '30003'.
it1-key2 = '40004'.
it1-a = 300.
APPEND it1 TO it1.

it2-key1 = '10001'.
it2-key2 = '20002'.
it2-b = 180.
APPEND it2 TO it2.
it2-key1 = '30003'.
it2-key2 = '40004'.
it2-b = 563.
APPEND it2 TO it2.

LOOP AT it1 INTO it1.
  MOVE-CORRESPONDING it1 to it3.
  READ TABLE it2 INTO it2 WITH KEY key1 = it1-key1 key2 = it1-key2.
  IF sy-subrc EQ 0.
    it3-b = it2-b.
  ENDIF.
  APPEND it3 TO it3.
  CLEAR: it1, it2, it3.
ENDLOOP.

LOOP AT it3 INTO it3.
  WRITE: / it3-key1, it3-key2, it3-a, it3-b.
ENDLOOP.

Kind Regards,

Faisal

Read only

Former Member
0 Likes
2,467

Try this way


sort itab1 by belnr.
loop at itab.
read table itab1 into temp with key belnr = itab-belnr Binary search.
if sy-subrc = 0.
move: itab-belnr to wa-belnr,
      itab-matnr to wa-matnr,
       temp-hwbas to wa-hwbas.
append wa to itab2.
clear wa. 
endif.

endloop.

Read only

Former Member
0 Likes
2,467

Hi Ankita,

Unfortunately direct appending the columns is not possible. We need to write a logic for achieving your requirement:

Somewhat like this:

belnr matnr ITAB1

100001 230000

100002 230001

and itab : ITAB2

belnr hwbas.

100001 2387.90

100002 980.00

RESULT in ITAB3.

loop at itab1.

move-corresponding itab1 to itab3.

read table itab2 with key matnr = itab1-matnr.

if sy-subrc = 0.

move-corresponding itab1 to itab3.

endif.

append itab3.

endloop.

Try this.

Regards,

Prakash Pandey

Read only

Former Member
0 Likes
2,467

hi ankita,

first do like this..

loop at itab into wa.

read table itab1 with key belnr = itab-belnr.

endloop.

write: / belnr,..///

i think it will work

Read only

Former Member
0 Likes
2,467

Hi Ankita,

You cannot directly append two internal tables data into the third table you have to ,

loop at itab1.

read table itab2 with key itab2-field = itab1-field.

move: itab1-fields to itab2-fields.

append itab2.

endloop.

loop at itab2.

read table itab3 with key itab3-field = itab2-field.

move: itab2-fields to itab3-fields.

append itab3.

endloop.

Hope it helps

Regrds

Mansi

Read only

Former Member
0 Likes
2,467

Hi,

Declare table itab2 with fields belnr, matnr and hwbas and use the below code to get the 3rd table itab.

clear ITAB.

loop at itab.

clear itab1.

read table itab1 with key belnr = itab-belnr

binary search.

if sy-subrc = 0.

Itab2-belnr = itab-belnr.

Itab2-matnr = itab-matnr.

Itab2-hwbas = itab1-hwbas.

append itab2.

clear itab2.

endif.

endloop.

Best Regards,

Deepa Kulkarni

Read only

Former Member
0 Likes
2,467

Hi,

Please look at the example in the below link it might be helpful,

http://www.sap-basis-abap.com/abap/how-to-add-two-internal-tables-in-third-internal-table.htm

Thank U,

Jay....

Read only

Former Member
0 Likes
2,467

hi,

use for all entries to fetch the required data.

Try to follow this code:

SELECT vbeln

waerk

fkdat

kunag

FROM VBRK INTO TABLE it_vbrk WHERE vbeln IN S_VBELN.

if it_vbrk is not initial.

SELECT vbeln

matnr

arktx

ntgew

gewei

netwr

werks

FROM vbrp

INTO TABLE it_vbrp

FOR ALL ENTRIES IN it_vbrk

WHERE vbeln EQ it_vbrk-vbeln.

endif.

LOOP AT it_vbrk INTO wa_vbrk.

MOVE wa_vbrk-vbeln TO wa_final0-vbeln.

MOVE wa_vbrk-waerk TO wa_final0-waerk.

MOVE wa_vbrk-fkdat TO wa_final0-fkdat.

MOVE wa_vbrk-kunag TO wa_final0-kunag.

READ TABLE it_vbrp INTO wa_vbrp WITH KEY wa_vbrk-vbeln.

MOVE wa_vbrp-matnr TO wa_final0-matnr.

MOVE wa_vbrp-arktx TO wa_final0-arktx.

MOVE wa_vbrp-ntgew TO wa_final0-ntgew.

MOVE wa_vbrp-gewei TO wa_final0-gewei.

MOVE wa_vbrp-netwr TO wa_final0-netwr.

MOVE wa_vbrp-werks TO wa_final0-werks.

APPEND wa_final0 TO it_final0.

CLEAR wa_final0.

ENDLOOP.

i have done it for billing purpose.

Solve ur problem by reffering this code.

Cheers

Read only

faisalatsap
Active Contributor
0 Likes
2,467

HI,

Test the Sample Code Bellow Hope will Solve out your problem,

DATA: BEGIN OF it1 OCCURS 10,
  key(10),
  a TYPE i,
 END OF it1.
DATA: BEGIN OF it2 OCCURS 10,
  key(10),
  b TYPE i,
 END OF it2.
DATA: BEGIN OF it3 OCCURS 10,
 key(10),
 b TYPE i,
 a TYPE i,
END OF it3.

it1-key = '10001'.
it1-a = 1000.
APPEND it1 TO it1.
it2-key = '10001'.
it2-b = 2000.
APPEND it2 TO it2.
it1-key = '10002'.
it1-a = 2000.
APPEND it1 TO it1.
it2-key = '10002'.
it2-b = 4000.
APPEND it2 TO it2.

LOOP AT it1 INTO it1.
  MOVE-CORRESPONDING it1 TO it3.
  READ TABLE it2 INTO it2 WITH KEY key = it1-key.
  IF sy-subrc EQ 0.
    MOVE-CORRESPONDING it2 TO it3.
  ENDIF.
  APPEND it3 TO it3.
  CLEAR: it1-a, it2-b, it3.
ENDLOOP.

LOOP AT it3 INTO it3.
  WRITE: / it3-key, it3-a, it3-b.
ENDLOOP.

Please Reply if any Issue,

Kind Regards,

Faisal

Read only

Former Member
0 Likes
2,467

Hi Ankita,

Follow the following code. Just copy the code and change the internal tables according to ur naming standards. While copying the code try to analyse it, bcs if you miss any clear statements it may yield to wrong results.

Loop at itab into wa_itab.

wa_itab2-belnr = wa_itab-belnr.

wa_itab2-matnr = wa_itab-matnr.

Read table itab1 into wa_itab1 with key belnr = wa_itab-belnr.

If sy-subrc = 0.

wa_itab2-hwbas = wa_itab1-hwbas.

endif.

append wa_itab2 to itab2.

clear wa_itab2.

clear wa_itab1.

clear wa_itab.

endloop.

Read only

Former Member
0 Likes
2,467

Hi ankita,

Try using the code beow,

I have checked and it is working

TYPES: BEGIN OF T_ITAB,

BELNR(6) TYPE C,

MATNR(6) TYPE C,

END OF T_ITAB.

TYPES: BEGIN OF T_ITAB1,

BELNR(6) TYPE C,

HWBAS(7) TYPE C,

END OF T_ITAB1.

TYPES: BEGIN OF T_ITAB2,

BELNR(6) TYPE C,

MATNR(6) TYPE C,

HWBAS(7) TYPE C,

END OF T_ITAB2.

*************************************************************

DATA: ITAB TYPE STANDARD TABLE OF T_ITAB,

ITAB1 TYPE STANDARD TABLE OF T_ITAB1,

ITAB2 TYPE STANDARD TABLE OF T_ITAB2,

WA_ITAB LIKE LINE OF ITAB,

WA_ITAB1 LIKE LINE OF ITAB1,

WA_ITAB2 LIKE LINE OF ITAB2.

*************************************************************

WA_ITAB-BELNR = '100001'.

WA_ITAB-MATNR = '230000'.

APPEND WA_ITAB TO ITAB.

CLEAR WA_ITAB.

WA_ITAB-BELNR = '100002'.

WA_ITAB-MATNR = '230001'.

APPEND WA_ITAB TO ITAB.

CLEAR WA_ITAB.

*****************************

WA_ITAB1-BELNR = '100001'.

WA_ITAB1-HWBAS = '2387.90'.

APPEND WA_ITAB1 TO ITAB1.

CLEAR WA_ITAB1.

WA_ITAB1-BELNR = '100002'.

WA_ITAB1-HWBAS = '980.00'.

APPEND WA_ITAB1 TO ITAB1.

CLEAR WA_ITAB1.

***************************************************8

LOOP AT ITAB INTO WA_ITAB.

WA_ITAB2-BELNR = WA_ITAB-BELNR.

WA_ITAB2-MATNR = WA_ITAB-MATNR.

APPEND WA_ITAB2 TO ITAB2.

CLEAR WA_ITAB2.

ENDLOOP.

LOOP AT ITAB1 INTO WA_ITAB1.

WA_ITAB2-HWBAS = WA_ITAB1-HWBAS.

MODIFY ITAB2 FROM WA_ITAB2

TRANSPORTING HWBAS

WHERE BELNR = WA_ITAB1-BELNR.

CLEAR WA_ITAB2-HWBAS.

ENDLOOP.

Please let me know if the problem is solved or if I am wrong.

Thanks and regards

Suraj

Read only

Former Member
0 Likes
2,467

Hi Ankit,

Try following code.

DATA : BEGIN OFitab2 OCCURS 0 WITH HEADER LINE,

belnr LIKE itab-belnr,

matnr LIKE itab-matnr ,

hwbas LIKE itab1-hwbas ,

END OF itab2.

data: l_index like sy-tabix.

l_index = 1.

SORT itab BY belnr.

SORT itab1 BY belnr.

****For better performance use always LOOP FROM INDEX*****

LOOP AT itab.

LOOP AT itab1 from l_index.

IF itab1-belnr EQ itab-belnr.

itab2-belnr = itab-belnr.

itab2-matnr = itab-matnr.

itab2-hwbas = itab1-hwbas.

append itab2.

clear itab2.

ELSEIF itab1-belnr GT itab-belnr.

MOVE sy-tabix to l_index.

EXIT.

ENDIF.

ENDLOOP.

ENDLOOP.

Hope this helps you.

Regards,

Anil

Read only

Former Member
0 Likes
2,467

Hi Ankita,

The below code will solve your problem.

LOOP AT itab INTO wa_tab.
  "Move the values from itab1 current line
  MOVE-CORRESPONDING wa_tab TO wa_tab2.
  
  "Get the line from itab1 for the current belnr
  READ TABLE itab1 INTO wa_tab1 WITH KEY belnr = wa_tab-belnr.
  
  "Assign hwbas value for the current line
  wa_tab2-hwbas = wa_tab1-hwbas.
  
  "Now wa_tab2 contains all the required values.
  APPEND wa_tab2 TO itab2.
ENDLOOP.

Hope this helps you.

Regards,

Manoj Kumar P

Read only

Former Member
0 Likes
2,468

" Add MATNR Field in also itab1. Then change that line of code
Loop at itab.
itab2-belnr = itab-belnr.
itab2-matnr = itab-matnr.
Read table itab1 with key belnr = itab-belnr matnr = itab-matnr. " change this line of code
If sy-subrc = 0.
itab2-hwbas = itab1-hwbas.
endif.
append itab2.
clear itab2.
clear itab1.
clear itab.
endloop.

Edited by: tahir naqqash on Feb 24, 2009 4:25 PM

Read only

0 Likes
2,467

Hi tahir...

Matnr filed is not in itab1 ...

Please help me............

Read only

0 Likes
2,467

as u filled itab field in itab whille selecting data in same way select matnr field for itab1.

but one more thing if both table have the same no of record then following logic can work but it is not appropriate for solution. but check it out..

sort itab by belnr.
sort itab1 by belnr.

loop at itab.
 itab2-belnr = itab-belnr. 
 itab2-matnr = itab-matnr.
 read table itab1 index sy-tabix.
 itab2-hwbas = itab1-hwbas.
 append itab2.
 clear: itab , itab1 , itab2.
endloop.

Read only

0 Likes
2,467

HI, Ankita,

You must have all key Field in both of Internal Tables for Correct Result in the 3rd internal table. Test my Sample code above, Hope will solve out your problem,

Kind Regards,

Faisal

Read only

Former Member
0 Likes
2,467

Hi,

Look Ankit, from wherever u r getting the two internal table (itab and itab1) there must be some other fields like buzei, gjahr. then try to fetch those fields into your itab and itab1.

after that u can match the two tables..

Sukriti..

Read only

Former Member
0 Likes
2,467

Hi Ankita,

You can try this.


loop at itab into wa_itab.

move: wa_itab-belnr   to wa_itab2-belnr,
          wa_itab-matnr to wa_itab2-matnr.

read table itab1 into wa_itab1 with key belnr = wa_itab-belnr.

if sy-subrc eq 0.

move: wa_itab1-hwbas to wa_itab2-hwbas.

endif.

append wa_itab2 to itab2.

clear: wa_itab,
         wa_itab1,
         wa_itab2.

endloop.

where wa_itab, wa_itab1, wa_itab2 are the work areas declared with the same structures as itab, itab1, itab2 respectively.

Read only

Former Member
0 Likes
2,467

Hi Ankita,

types:

begin of type_s_tab3,

belnr type....,

matnr type.....,

hwbas type.....,

end of type_s_tab3.

data: fs_tab3 type type-s_tab3.

data itab3 like table of fs_tab3 with header line.

sort itab1 by belnr.

sort itab2 by belnr.

loop at itab1.

itab3-belnr = itab1-belnr.

itab3-matnr = itab1-matnr.

loop at itab2.

itab3-hwbas = itab2-hwbas.

append itab3.

endloop.

endloop.

Hope it helps....

Regards,

Mdi.Deeba.