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

loop over two internal tables

Former Member
0 Likes
5,732

hello,

I have two tables. one table has 2 lines and the other has also 2 lines. i want to have for all data in table 1 the data which is in table 2, this means:

table 1

customer 1

customer 2

table 2

matnr1

matnr2

result table

customer1 matnr1

customer1 matnr2

customer2 matnr1

customer2 matnr2

how can I get the result table with loop.?

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,128

Hi,

Check this

Loop at table1.
Loop at table2.
table3-customer = table1-customer.
table3-matnr      =   table2-matnr.
Append table3.
clear table3.
Endloop.
endloop.

you will get the result as you required.

8 REPLIES 8
Read only

Former Member
0 Likes
2,128

table3

customer

matnr

loop at table1.

clear table2.

read table2 with index sy-tabix.

table3-customer = table1-customer

table3-matnr = table2-matnr.

append table3. clear table3.

endloop.

Read only

Former Member
0 Likes
2,129

Hi,

Check this

Loop at table1.
Loop at table2.
table3-customer = table1-customer.
table3-matnr      =   table2-matnr.
Append table3.
clear table3.
Endloop.
endloop.

you will get the result as you required.

Read only

0 Likes
2,128

why did you clear table3 Avinash?

Read only

0 Likes
2,128

It is not required in this case. I have the habit of clearing the header data after append statement.

In some case where you use the read statement and populate some of the table fields in the loop. In this case when read cannot return the values for the givn condt then you need to pass blank to those fields. If you donot clear the values the previous values will exit.

Example.

LOOP AT i_9121 INTO i_9121_line.
    CLEAR i_hrsbu_line.

" First loop pass the read returns the value and maaping the value to the internal table field 
" i_vdetails_line-stext. But in the Next pass the read did not returned the value and sy-subrc get's 
" failed and value is not mapped to i_vdetails_line-stext. If you are not clearing the previous loop pass
" exits and get's append's..which is wrong
    READ TABLE i_hrsbu INTO i_hrsbu_line
             WITH KEY hrsbu = i_9121_line-hrsbu BINARY SEARCH.
    IF sy-subrc EQ 0.
      i_vdetails_line-stext =  i_hrsbu_line-stext.
    ENDIF.

    i_vdetails_line-unit   =  i_9121_line-persa(2).
    i_vdetails_line-objid  =  i_9121_line-objid.
    i_vdetails_line-posti  =  i_9121_line-posti.
    i_vdetails_line-begda  =  i_9121_line-begda.

    CLEAR i_funar_line.
    READ TABLE i_funar_t INTO i_funar_line
             WITH KEY funar = i_9121_line-funar BINARY SEARCH.
    IF sy-subrc EQ 0.
      i_vdetails_line-ltext =  i_funar_line-ltext.
    ENDIF.

*   Get Position Request Status Details
    CLEAR i_9122_line.
    READ TABLE i_9122 INTO i_9122_line
         WITH KEY objid = i_9121_line-objid BINARY SEARCH.
    IF sy-subrc EQ 0.
      CLEAR i_itmcd_line.
      READ TABLE i_itmcd_t INTO i_itmcd_line
                WITH KEY itmcd = i_9122_line-rstat BINARY SEARCH.
      IF sy-subrc EQ 0.
        i_vdetails_line-itext =  i_itmcd_line-itext.
      ENDIF.

      LOOP AT i_ev_st_lg INTO  i_st_lg_line
                         WHERE itmcd EQ i_9122_line-rstat  .
      ENDLOOP.
      IF sy-subrc EQ 0.
        CLEAR: l_date , l_time.
        WRITE :
          i_st_lg_line-datum TO l_date,
          i_st_lg_line-uzeit TO l_time.
        CONCATENATE l_date l_time(5) INTO i_vdetails_line-dandt
                         SEPARATED BY space.
      ENDIF.
    ENDIF.

*   Get Position Request Basic Details
    CLEAR i_9120_line.
    READ TABLE i_9120 INTO i_9120_line
         WITH KEY objid = i_9121_line-objid BINARY SEARCH.
    IF sy-subrc EQ 0.
      IF i_9120_line-intas EQ 'Y'.
        i_vdetails_line-intas = c_y.
      ELSEIF i_9120_line-intas EQ 'N'.
        i_vdetails_line-intas = c_n.
      ENDIF.
      IF NOT i_9120_line-projo IS INITIAL.

        PERFORM get_jobname USING i_9120_line-projo
                         CHANGING i_vdetails_line-jtitle.
      ENDIF.
    ENDIF.

    APPEND i_vdetails_line TO i_vdetails.
    CLEAR i_vdetails_line.
  ENDLOOP.

Read only

0 Likes
2,128

Avinash has cleared the header line of table 3 well to avoid the confusion use like this


Loop at table1 into wa1.
Loop at table2 into wa2.
wa3-customer = wa1-customer.
wa3-matnr      =   wa2-matnr.
Append wa3 to table3.
clear wa3.
Endloop.
endloop.

The above code is avinash's i only modified it to make it clearer to u.

Read only

Former Member
0 Likes
2,128

loop tab1 into wa1.

loop tab2 intowa2.

append wa3 to tab3.

endloop.

endloop.

Read only

Former Member
0 Likes
2,128

<<answer deleted as similar responses already posted>>

in the above responses only avinash's solution will work.

Edited by: kartik tarla on Jan 21, 2009 10:52 PM

Read only

Former Member
0 Likes
2,128

hi,

if u want to display the data like this when u fetch the data from the database table itself u can use outer join,otherwise if u want to process the internal tables,then u can go for the abouve method...