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

Populating one Internal table from two internal tables.

Former Member
0 Likes
3,282

Hello Gurus,

iam having 3 internal table with same structure ,

1st internal table having some records

2nd internal table is also having some records

what i need to do is to populate the records to the 3rd internal table which the common records in the 1st and 2nd internal table. (common means intersection).

please explain how and using which concept and if possible code also...

Regards,

Ravi.

Hello Gurus,

iam having 3 internal table with same structure ,

1st internal table having some records

2nd internal table is also having some records

what i need to do is to populate the records to the 3rd internal table which the common records in the 1st and 2nd internal table. (common means intersection).

please explain how and using which concept and if possible code also...

Regards,

Ravi.

8 REPLIES 8
Read only

Former Member
0 Likes
1,321

Can you send the structure of the internal tables and their primary keys and some model table contents. So that we can usderstand how the data should be populated from the first two to third.

Read only

0 Likes
1,321

Hi,

we can take a simple example student details

structure fields

Registration Number

Name

Address

group

end of structure.

all the three internal table having same structure.

1, How to do it in ordinary way?

2, How to do using the Field Symbols?

Regards,

Ravi.

Read only

Former Member
0 Likes
1,321

hi ravi,

look at the folowing code for reference

*&--mard structure

types: begin of struc_mard,

matnr type matnr, "Material Number

werks type werks_d, "Plant

lgort type lgort_d, "Storage Location

labst type labst, "Valuated stock with unrestricted use

end of struc_mard.

*&--mara structure

types: begin of struc_mara,

matnr type matnr, "material number

meins type meins, "unit of measurement

mtart type mtart, "material type

ersda type ersda, "Creation date

end of struc_mara.

*&--maktx structure

types: begin of struc_maktx,

matnr type matnr, "material number

maktx type maktx, "material description

end of struc_maktx.

*&--Final structure

types: begin of struc_final,

matnr type matnr, "Material Number

werks type werks_d, "Plant

lgort type lgort_d, "Storage Location

labst type labst, "Valuated stock with unrestricted use

meins type meins, "unit of measurement

mtart type mtart, "material type

ersda type ersda, "Creation date

maktx type maktx, "material description

v_box(1) type c, "for checkbox field

end of struc_final.

*&---work area for Internal Tables[population 1]

data: wa_mard type struc_mard,

wa_mara type struc_mara,

wa_maktx type struc_maktx,

wa_final type struc_final.

----


  • INTERNAL TABLES DECLARATION *

----


*&---Internal tables for storing data.[populaiton 1]

data: i_mard type standard table of struc_mard,

i_mara type standard table of struc_mara,

i_maktx type standard table of struc_maktx,

i_final type standard table of struc_final.

*&--fetching the data from table for storage

select matnr

werks

lgort

labst

into table i_mard

from mard

where matnr in r_matnr and werks in r_werks and lgort in

r_lgort

.

if sy-subrc <> 0. "if unsuccesful

message e004. "Error- Record does not exist

endif.

  • selection of material description

if i_mard[] is not initial.

*SELECT DISTINCT FOR UNIQUE ENTRIES ONLY

select distinct matnr

maktx

into table i_maktx

from makt

for all entries in i_mard

where matnr = i_mard-matnr and

spras = sy-langu.

if sy-subrc <> 0. " if unsucessful

wa_maktx-maktx = text-028. "NO DESCRIPTION

endif.

*fetching information from material master

select distinct matnr

meins

mtart

ersda

into table i_mara

from mara for all entries in i_mard

where matnr = i_mard-matnr.

if sy-subrc <> 0.

message e003.

endif.

*&--sorting the tables

sort: i_mard by matnr werks lgort,

i_mara by matnr,

i_maktx by matnr.

*&--Clearing workareas.

clear: wa_mard,

wa_mara,

wa_maktx,

wa_final.

*PROCESSING

loop at i_mard into wa_mard.

*moving values to final workarea

wa_final = wa_mard.

at new matnr.

read table i_maktx into wa_maktx

with key matnr = wa_mard-matnr binary search.

read table i_mara into wa_mara

with key matnr = wa_mard-matnr binary search.

endat.

if sy-subrc = 0.

"if succesful then move to final workarea

wa_final-maktx = wa_maktx-maktx.

*for mara values

wa_final-meins = wa_mara-meins.

wa_final-mtart = wa_mara-mtart.

wa_final-ersda = wa_mara-ersda.

endif.

*append final workarea

append wa_final to i_final.

endloop.

  • clearing and refreshing the table I_MARD

refresh: i_mard.

clear: wa_mard.

endif.

endform. "zf_populate_info

this will move all the data into final table you have got, with the performnce tuned way

reward point if useful

Rohan malik

Read only

Vijay
Active Contributor
0 Likes
1,321

hi

try this way

-


loop at itab1 into wa1

read table itab2 with key a = wa1-a b = wa1-b.

if sy-subrc = 0.

appand wa1 to itab3.

endif.

endloop.

-


regards

vijay

reward points if helpful

Read only

Former Member
0 Likes
1,321

Hi,

As you have told the structure is same for the three tables, you can proceed in this manner.

Loop at itab1.

Read table itab2 into wa_itab3 where itab2-<f1> = itab1-<f1> and so on…. ( for all the common fields which you want to select )

append wa_itab3 to itab3.

Reward if helpful.

Read only

Former Member
0 Likes
1,321

ANSWER

LOOP AT ITAB1

READ TABLE ITAB2 WITH KEY FIELD1 = ITAB1-FIELD1.

IF SY-SUBRC = 0

APPEND ITAB1 TO ITAB3.

ENDLOOP.

Assume that ITAB1, ITAB2, ITAB3 ARE ITABS WITH HEADER LINES

Reward if useful

Narendra

Read only

Former Member
0 Likes
1,321

sort both the tables.

write loop at first internal table and

if second internal table is having single record then use read statement with where condition field of first internal table .

use binary search i fyou use read statemetn.

if second internal table is having multiple records for the single value of the first internal table then use loop for second internal table with where condition.

append to third internal table.

sort: itab1,itab2.

in case of single record in second table then

loop at itab1.

read itab2 with key <fld> = itab1-fld binary search.

if sy-subrc eq 0.

move fields of itab1 and itab2 to itab3

append itab3.

endif.

endloop.

if tab2 is having multiple records then

loop at itab1.

loop at itab2 where fld = itab1-fld

move fields of itab1 and itab2 to itab3

append itab3.

endloop.

endloop.

Read only

0 Likes
1,321

if iam looping the first itab1 then it is having 4000 records,

then reading the second itab2 is having 10 records,

unnecessarly loop wl run 4000 times ,

what wl be the optimistic way, is that filed symbols can play anything ?