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

add two internal tables

Former Member
0 Likes
9,067

hi

how can we merge two internal table in a single internal table.

please give me the syntax.

hi

how can we merge two internal table in a single internal table.

please give me the syntax.

6 REPLIES 6
Read only

former_member404244
Active Contributor
0 Likes
3,697

Hi,

There are many ways

one way is

loop at itab1.

read table itab2 with key field1 = itab1-field1.

if sy-subrc eq 0.

move-corresponding itab1 to iatb3.

move-corresponding itab2 to itab3.

append itab3.

clear itab3.

endif.

endloop.

Regards,

Nagaraj

Read only

Former Member
0 Likes
3,697

try one among following :

1)

you can use this:

append lines of itab1 to it_final.

append lines of itab2 to it_final.

....

2)

loop at itab1.

move-corresponding itab1 to it_final. "For same field name purpose

it_final-field1 = itab1-field2. "For different field name purpose

append it_final.

endloop.

loop at itab2.

move-corresponding itab2 to it_final. "For same field name purpose

it_final-field2 = itab2-field2. "For different field name purpose

append it_final.

endloop.

3)

data declaration part

TABLES: mara,marc,mard .

**********TYPES

TYPES: BEGIN OF t_mara,

matnr TYPE matnr, "Material Number

mtart TYPE mtart, "Material Type

meins TYPE meins, "Basic Unit Of measure

END OF t_mara.

TYPES: BEGIN OF t_marc,

matnr TYPE matnr, "Material Number

werks TYPE werks_d, "Plant

END OF t_marc.

TYPES: BEGIN OF t_mard,

matnr TYPE matnr,

werks TYPE werks_d,

lgort TYPE lgort_d,

END OF t_mard.

TYPES: BEGIN OF t_tab,

matnr TYPE matnr,

mtart TYPE mtart,

meins TYPE meins ,

werks TYPE werks_d ,

lgort TYPE lgort_d,

END OF t_tab.

**********WORKAREA

DATA: wa_mara TYPE t_mara,

wa_marc TYPE t_marc,

wa_mard type t_mard,

wa_tab TYPE t_tab,

e_matnr TYPE matnr,

wa_matnr TYPE matnr.

DATA : i_fieldcat TYPE slis_t_fieldcat_alv.

DATA: d_field(40), d_value(40),

AMBER(20).

**********INTERNAL TABLES

DATA: it_mara TYPE STANDARD TABLE OF t_mara,

it_marc TYPE STANDARD TABLE OF t_marc,

it_mard TYPE STANDARD TABLE OF t_mard,

it_tab TYPE STANDARD TABLE OF t_tab.

Data Selection Part

*select data from mara table

SELECT matnr

mtart

meins

FROM mara

INTO TABLE it_mara

WHERE matnr IN s_matnr.

*select data from marc table

SELECT matnr

werks

FROM marc

INTO TABLE it_marc

FOR ALL ENTRIES IN it_mara

WHERE matnr = it_mara-matnr.

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

THIS BLOCK MOVES THE FIELDS FROM DIFFERENT *

TABLES(mara,marc) INTO A FINAL TABLE IT_TAB *

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

LOOP AT it_marc INTO wa_marc .

READ TABLE it_mara INTO wa_mara WITH KEY matnr = wa_marc-matnr BINARY SEARCH.

IF sy-subrc = 0 .

wa_tab-matnr = wa_mara-matnr .

wa_tab-mtart = wa_mara-mtart.

wa_tab-meins = wa_mara-meins.

ENDIF.

wa_tab-werks = wa_marc-werks .

APPEND wa_tab TO it_tab.

ENDLOOP.

Read only

former_member156446
Active Contributor
3,697

if both the internal tables have same structure you can add

using

append lines of itab1 to itab2. " good performance

Read only

Former Member
0 Likes
3,697

hi phanidar

check the code this may helps in clarifying your doubt

TABLES : MARA, MAKT.

SELECT-OPTIONS : SO_MATNR FOR MARA-MATNR .

DATA : BEGIN OF IT_MARA OCCURS 0,

MATNR LIKE MARA-MATNR,

MTART LIKE MARA-MTART,

END OF IT_MARA,

BEGIN OF IT_MAKT OCCURS 0,

MATNR LIKE MARA-MATNR,

MAKTX LIKE MAKT-MAKTX,

END OF IT_MAKT,

BEGIN OF JTAB OCCURS 0,

MATNR LIKE MARA-MATNR,

MTART LIKE MARA-MTART,

MAKTX LIKE MAKT-MAKTX,

END OF JTAB.

START-OF-SELECTION.

SELECT MATNR MTART

FROM MARA

INTO TABLE IT_MARA

WHERE MATNR IN SO_MATNR.

SELECT MATNR MAKTX

FROM MAKT

INTO TABLE IT_MAKT

WHERE MATNR IN SO_MATNR

AND SPRAS = 'E'.

LOOP AT IT_MARA.

MOVE IT_MARA-MATNR TO JTAB-MATNR.

MOVE IT_MARA-MTART TO JTAB-MTART.

APPEND JTAB.

ENDLOOP.

CLEAR IT_MARA.

SORT JTAB BY MATNR.

SORT IT_MAKT BY MATNR.

LOOP AT IT_MAKT.

READ TABLE JTAB WITH KEY MATNR = IT_MAKT-MATNR.

IF SY-SUBRC EQ 0.

JTAB-MAKTX = IT_MAKT-MAKTX.

MODIFY JTAB INDEX SY-TABIX.

ENDIF.

ENDLOOP.

LOOP AT JTAB.

WRITE 😕 JTAB-MATNR,

JTAB-MTART,

JTAB-MAKTX.

ENDLOOP.

Regards

sachin

Read only

kesavadas_thekkillath
Active Contributor
0 Likes
3,697


While merging there must be key field existing the two internal tables.


either u can create a new internal table and dump the data of the two itabs to it.
or u can modify the primary table(itab1) from the secondary table(itab2)


1) itab1 has field matnr mtart
2) itab 2 has field matnr werks


loop at itab1.
read table itab2 with key matnr = itab1-matnr.
check sy-subrc = 0.
itab3-matnr = itab1-matnr.
itab3-mtart = itab1-mtart.
itab3-werks = itab2-werks.
append itab3.
endloop.

it can be done also like this.

put a field werks in itab1 and modify it.

loop at itab1.
read table itab2 with key matnr = itab1-matnr.
check sy-subrc = 0.
itab1-werks = itab1-werks.
modify itab1 transporting werks.
append itab3.
endloop.


the above statements can be used if u r sure that the inner join condition exists in the tables.


if the itab2 has more than one entries for the matnr in itab 1
( ex: more than one werks for a matnr).

Then use this.

loop at itab1.
Loop at itab2 where matnr = itab1-matnr.
itab3-matnr = itab1-matnr.
itab3-mtart = itab1-mtart.
itab3-werks = itab2-werks.
append itab3.
endloop.
endloop.



reward if usefull :-)



Read only

Former Member
0 Likes
3,697

Hi

Simple Use append ines of itab1 to itab2.