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: 

Select and Data declaration issue

amine_lamkaissi
Active Contributor
0 Kudos

Hi experts,

I am facing some issues with abap syntax and I need your clarifications. Let’s say I declared an internal table (mytable) in my program with 2 columns (Column1 and Column2)

I want to fill those fields from different tables (Table 1 and Table 2), to simplify my example:

Table 1

Field1

Field2

Field3

1

2

Table 2

Field1

Field2

Field3

1

2

3

I want the content of Field1 – Table 1 in Column1 and the content of Field2 – Table2 in Column2.

So normally the logic would be:

Select Field 1 from Table 1 into mytable–Column1.

Select Field2 from Table2 into mytable–Column2.

I’ve tried some combinations but it fails certainly because of my declarations. What I expect in mytable is the following:

Column1

Column2

1

1

2

2

3

Thanks for your explanations.

Amine

1 ACCEPTED SOLUTION

former_member205060
Active Participant
0 Kudos

I don't think there is much pain in the logic.

Loop at ITAB1.

clear count.

count = sy-tabix.

col1-final_tab = tab1-field.

read itab2 at index count.

col2-final_tab = tab2-field.

append finaltab.

endloop.

loop at itab1.

if count LT sy-tabix.

col1-final_tab = tab1-field.

append finaltab.

endif.

endloop.

loop at itab2.

if count LT sy-tabix.

col2-final_tab = tab2-field.

append finaltab.

endif.

endloop.

there can be Optimization problem in that case you can start your loop directly from  Index Value where count has ended.

Regards,

Rahul

14 REPLIES 14

former_member186077
Active Participant
0 Kudos

Hi Amine,

Please see the below sample code.

Hope it helps.

DATA : BEGIN OF itab1 OCCURS 0,

        col1 TYPE c,

        col2 TYPE c,

        END OF itab1,

        BEGIN OF itab2 OCCURS 0,

        col1 TYPE c,

        col2 TYPE c,

        END OF itab2,

        BEGIN OF itab_final OCCURS 0,

        col1 TYPE c,

        col2 TYPE c,

        END OF itab_final.

DO 3 TIMES.

   itab1-col1 = sy-index.

   itab2-col2 = sy-index.

   APPEND itab1.

   APPEND itab2.

ENDDO.

LOOP AT itab1.

   READ TABLE itab2 INDEX sy-tabix.

   itab_final-col1 = itab1-col1.

   itab_final-col2 = itab2-col2.

   append itab_final.

   write itab_final.

ENDLOOP.

0 Kudos

Hi Sriranjani,

Thanks for your answer.

But i am not supposed to know the number of records (Do 3 times), it has to be dynamic.

Table1 and Table2 are SAP tables and may change everyday.

Amine

0 Kudos

Hi Amine,

I have done do loop only to add some entries into the internal table.

In your case you shall have some values from the respective select tables.

Thanks and Regards,

Sriranjani Chimakurthy.

Former Member
0 Kudos

Hi,

Is there a relationship between table1-field1 and table2-field2?  From your (otherwise detailed) explanation there doesn't seen to be.

The relationship between the two tables will influence how you populate your internal table.

Regards,

Nick

0 Kudos

Hi Nick,

Actually, there is no relationship between table1-field1 and table2-field2.

Thanks.

Amine

0 Kudos

Hi Amine,

I'm not usre I undertand why you need to combine unrelated data into the same row of an internal table, but the pseudo code to achieve it might go something like this;

Select Field1 from Table1 into mytable1–Column1.

Select Field2 from Table2 into mytable2–Column1.

do while .

read table mytable1 index counter into wa_combined_table-column1.

read table mytable2 index counter into wa_combined_table-column2.

if wa_combined_table is not initial.

  append wa_combined_table to combined_table.

  add 1 to counter.

else.

  exit.

endif.

enddo.

This should fill internal table combined_table.

regards,

Nick

Former Member
0 Kudos

Hi Amine,

  Try this solution

DATA:

   lv_flag1 TYPE bool,

   lv_flag2 TYPE bool.

DO.

   IF lv_flag1 = abap_false.

     READ TABLE itab1 INTO wa1 INDEX sy-index.

     IF sy-subrc <> 0.

         lv_flag1 = abap_true.  "Set true if no entry exists

     ENDIF.

   ENDIF.

  IF lv_flag2 = abap_false.

     READ TABLE itab2 INTO wa2 INDEX sy-index.

     IF sy-subrc <> 0.

        lv_flag2 = abap_true.  "Set true if no entry exists

      ENDIF.

   ENDIF.

* Exit if no entries exists from both internal table

  IF lv_flag1 = abap_true AND lv_flag2 = abap_true.

     EXIT.

   ENDIF.

  wa3-field1 = wa1-field1.

   wa3-field2 = wa2-field2.

   APPEND wa3 TO itab3.

  CLEAR: wa1, wa2, wa3.

ENDDO.

Hope it was helpful.

former_member205060
Active Participant
0 Kudos

I don't think there is much pain in the logic.

Loop at ITAB1.

clear count.

count = sy-tabix.

col1-final_tab = tab1-field.

read itab2 at index count.

col2-final_tab = tab2-field.

append finaltab.

endloop.

loop at itab1.

if count LT sy-tabix.

col1-final_tab = tab1-field.

append finaltab.

endif.

endloop.

loop at itab2.

if count LT sy-tabix.

col2-final_tab = tab2-field.

append finaltab.

endif.

endloop.

there can be Optimization problem in that case you can start your loop directly from  Index Value where count has ended.

Regards,

Rahul

0 Kudos

The other way around is.

DESCRIBE TABLE ITAB1 LINES1.

DESCRIBE TABLE ITAB2 LINES2.

If Lines1 is GE Lines2.

  Loop at itab1.

      read itab2 with index sy-tabix.

      itab1-col2 = itab2-col1.

      modify itab1.

      endloop.

else.

       Loop at itab2.

       read itab1 with index sy-tabix.

       if sy-subrc = 0.

           itab1-col2 = itab2-col1.

           modifi itab1 index sy-tabix.

           else.

           itab1-col2 = itab2-col1.

           Append Itab1.

       endif.

  endloop.

endif.

custodio_deoliveira
Active Contributor
0 Kudos

Hi Amine,

I think you guys are walking in circles with loops, dos and whiles but a couple of simple selects would do the trick (if I understood correctly )

Have you tried this yet?

data:

  begin of mytable,

     column1 type table of field1,

    column2 type table of field2,

   end of mytable.

Select Field1 from Table 1 into table mytable–Column1.

Select Field2 from Table2 into table mytable–Column2.

mytable is actualy a deep structure with 2 table types. you do 2 selects into tables.

Regards,

Custodio

0 Kudos

Hi Custodio,

I guess all the DOing and LOOPing is to accomodate a perceived need to read each column from the extracted data as a pair, otherwise there wouldn't be a need to put them in the same internal table.

But as I can't explain why you would want to combine two unrelated sets of data into the same internal table and process them in the way I guess I've not the definitive view.

regards,

Nick

0 Kudos

Hi Nick,

Thanks for that. Yes, probably neither of us got the picture of what Amine is trying/needs to do.

Cheers,

Custodio

Former Member
0 Kudos

you can use below logic.

    CLEAR: lv_countecc.

           lv_countecc = 0.

    LOOP AT gt_docno INTO gs_docno.
        CLEAR: gs_output.
        gs_output-docno_ecc = gs_docno-docno.
        gs_output-itemcount_ecc = gs_docno-itemcount.
        APPEND gs_output TO gt_output.
        lv_countecc = lv_countecc + 1.
    ENDLOOP.

    CLEAR: lv_countbw.
           lv_countbw = 1.

    LOOP AT lt_docno_bw_count INTO ls_docno_bw_count.

      IF lv_countbw LE lv_countecc.
        gs_output-docno_bw = ls_docno_bw_count-doc_number.
        gs_output-itemcount_bw = ls_docno_bw_count-itemcount.
        MODIFY gt_output FROM gs_output INDEX lv_countbw
                         TRANSPORTING docno_bw itemcount_bw.
        lv_countbw = lv_countbw + 1.
      ELSE.
        CLEAR: gs_output.
        gs_output-docno_bw = ls_docno_bw_count-doc_number.
        gs_output-itemcount_bw = ls_docno_bw_count-itemcount.
        APPEND gs_output TO gt_output.
      ENDIF.

    ENDLOOP

amine_lamkaissi
Active Contributor
0 Kudos

Thanks guys for your interesting and rich answers.

Amine