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

populate internal table from 2 tables

Former Member
0 Likes
5,010

hi i want to create an internal table having one field from first standard table and all fields from second internal table

and then populate that internal table.

hi i want to create an internal table having one field from first standard table and all fields from second internal table

and then populate that internal table.

11 REPLIES 11
Read only

Kartik2
Contributor
0 Likes
3,094

Hi,

Create internal table A, fetch data from Standard table A.

Create internal table B, fetch data from Standard table B, if possible using for all entries in table A.

Create internal table C having fields from both the tables A and B. Now loop at internal table A, read Internal table B inside the loop and append records to internal table C.

Hope it helps. Thank you.

Regards,

Kartik

Read only

Former Member
0 Likes
3,094

Hi Rishabh,

With reference to Kartik's reply, if you use 'for all entries' on internal table A while selecting data from database table B, internal table B will be the table with your required structure. There is no need to loop on it any further.

@Kartik: Correct me if I am wrong, please.

Regards,

Gautam

Read only

Former Member
0 Likes
3,094

hi kartik how can i create internal table c having one field from first standard table and all fields from another standard table.

Read only

Former Member
0 Likes
3,094

Hi Rishabh,

Let table A have the structure:

AAAA,

BBBB,

CCCC,

DDDD.

Let table B have the structure:

AAAA

EEEE

FFFF

GGGG

Now loop at table A and read table B to create the table C as follows:

Sort ITABB by AAAA.

Loop at ITABA into WA_A.

Read table ITABB into WA_B with key AAAA = WA_A-AAAA.

If sy-subrc = 0.

Append WA_B to ITABC.

Endif.

Clear: WA_A, WA_B.

Endloop.

Regards,

Gautam

Read only

Former Member
0 Likes
3,094

how can i create internal table c having one field from first standard table and all fields from another standard table.

you can use INCLUDE b , to include all fields of b

TYPES BEGIN OF struc_type_c.

  ...

  {TYPES dtype ...} | {INCLUDE {TYPE|STRUCTURE} ...}.

  ...

TYPES END OF struc_type_c.

Read only

RaymondGiuseppi
Active Contributor
0 Likes
3,094

(FAQ) What did you try before posting, do you have any problem with SORT, LOOP AT, MODIFY or READ TABLE (BINARY SEARCH) statement ?

Regards,

Raymond

Read only

0 Likes
3,094

hi raymond i tried loop at and read table.
may be i m not clear

Read only

Former Member
0 Likes
3,094

hi gupta,

please do this way..

ex: 2 tables

mara, marc.

data : lt_mara like mara occurs 0 with header line.

data : lt_marc like marc occurs 0 with header line.

output table :

data :begin of output,

matnr like mara-matnr,

ernam like mara-ernam,

werks like marc-werks,

pstat like marc-pstat

end of output.

start-of selection

select * from mara  into corresponding fileds of lt_mara where matnr eq p_matnr

if sy-subrc = 0.

select * from marc form all entries in lt_mara into table lt_marc where matnr eq lt_mara-matnr.

endif.

end-of selection

loop at lt_mara.

read table lt_marc with key matnr eq lt_marc-matnr

move the values to output

append output.

endloop.

hope that helps...

thank you,

kata.

Read only

ThomasZloch
Active Contributor
0 Likes
3,094

basic, FAQ -> un-marked as question.


Thomas

Read only

Former Member
0 Likes
3,094

Hello,

One question... Your source tables and their fields changes in runtime? if that so, you have to use RTTS. If not, just define:

TYPES: BEGIN OF t_type,

   field_tab1 TYPE <data_elemnt_tab1>.

   INCLUDE STRUCTURE <table_name2>.

TYPES: END OF t_type.

TYPES: tt_type TYPE TABLE OF t_type.

Regards,

John.

Read only

Former Member
0 Likes
3,094

As what i think..

You have two internal tables and you want one field from ITAB_A and Rest all fields from ITAB_B.

Suppose your Standard tables A and B

Two methods are there :

1) Select filed1 from A into table ITAB_A where <Cond>.

       if ITAB_A is not initial .

          Select * from B into table ITAB_B for all entries in ITAB_A

                                                                             where field1 eq ITAB_A-field1.

      endif.

     Loop at ITAB_B into WA_B.

         <here you can pass your data to the final internal table for display >

         WA_Final-field1   = WA_B-Filed1.

        WA_Final-field2   = WA_B-Filed2

         .

         .

         .

        WA_Final-fieldn   = WA_B-Filedn.

         append wa_final to itab_final.

         clear : wa_final, wa_b.

     Endloop.

2) Suppose your field1 is not there in the table B.

    

    data : n1 type i,

              n2 type i.

     Select field1 from A into table ITAB_A where <Condition>.

     select * from B into table ITAB_B where <Condition>.

    DESCRIBE TABLE itab_A LINES n1." Count the number of records available for itab_a.

    DESCRIBE TABLE itab_B LINES n2." Count the number of records available for itab_b.

     if (n1 ge n2).

          loop at itab_a into wa_a.

              wa_final-field1 = wa_b-field1.

            read table itab_b into wa_b with key <condition for key field>.

             wa_final-field2 = wa_b-field2.

             wa_final-field3 = wa_b-field3.

             .

             .

             .

             wa_final-fieldn = wa_b-fieldn.

             append wa_final to itab_final.

             clear : wa_final,

                      wa_a, wa_b.

          endloop.

       else.

          loop at itab_b  into wa_b.

            wa_final-field2 = wa_b-field2.

            wa_final-field3 = wa_b-field3.

            .

            .

            .

            wa_final-fieldn = wa_b-fieldn.

          read table itab_a into wa_a with key <condition for key field>.

             wa_final-field1 = wa_b-field1.

          

            append wa_final to itab_final.

            clear : wa_final,

                     wa_a, wa_b.

          endloop.

    endif.

Please correct me if i am wrong anywhere.

Thanks and Regards,

Swarnadeepta.