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: 

Issue with nested loops. All line items were not appending

Former Member
0 Kudos

Hi Experts,

I have one issue.

I am looping one internal table (BKPF) and in that i am looping another internal table (BSEG).

But my problem is if there are 10 items for one BELNR then all the items from BSEG was not appending to the final internal table. only the last record was appending. Can any one provide me solution to this. I am pasting the code..

  LOOP AT tab_bkpf_selected_data INTO wa_bkpf_selected_data.
    wa_final-blart = wa_bkpf_selected_data-blart.
    wa_final-bldat = wa_bkpf_selected_data-bldat.
    wa_final-budat = wa_bkpf_selected_data-budat.

    LOOP AT tab_bseg INTO wa_bseg WHERE belnr = wa_bkpf_selected_data-belnr.
      wa_final-bukrs = wa_bseg-bukrs.
      wa_final-belnr = wa_bseg-belnr.
      wa_final-gjahr = wa_bseg-gjahr.
      wa_final-buzei = wa_bseg-buzei.
      wa_final-augbl = wa_bseg-augbl.
      wa_final-bschl = wa_bseg-bschl.
      wa_final-hkont = wa_bseg-hkont.
      wa_final-kunnr = wa_bseg-kunnr.
      wa_final-bupla = wa_bseg-bupla.

      IF wa_bseg-bschl = '40'.
        IF wa_bseg-koart = 'S'.
          IF wa_bseg-valut <> ' '.
            wa_final-valut = wa_bseg-valut.
            wa_final-koart = wa_bseg-koart.
            wa_final-zuonr = wa_bseg-zuonr.
          ENDIF.
        ENDIF.
      ENDIF.
    ENDLOOP.

    LOOP AT tab_bsad INTO wa_bsad WHERE belnr = wa_bkpf_selected_data-belnr.

*     Check that bsad-augbl is not equal to bsad-belnr.
      IF wa_bsad-augbl NE wa_bsad-belnr.

        wa_final-v_invoice_no = wa_bsad-belnr.

*       If bsad-blart EQ 'DR'
        IF wa_bsad-blart EQ c_dr.
          wa_final-v_inv_amt = wa_bsad-wrbtr.

*       If bsad-blart EQ 'DR'
        ELSEIF wa_bsad-blart NE c_dr.
          wa_final-v_adv_amt = wa_bsad-wrbtr.
        ENDIF.
      ENDIF.

    ENDLOOP.

    APPEND wa_final TO c_tab_final.

 ENDLOOP.

Thanks & Regards,

Ramana

Edited by: Matt on Nov 20, 2008 3:16 PM - see how nice tags make your code look

7 REPLIES 7

Former Member
0 Kudos

LOOP AT tab_bkpf_selected_data INTO wa_bkpf_selected_data.

wa_final-blart = wa_bkpf_selected_data-blart.

wa_final-bldat = wa_bkpf_selected_data-bldat.

wa_final-budat = wa_bkpf_selected_data-budat.

LOOP AT tab_bseg INTO wa_bseg WHERE belnr = wa_bkpf_selected_data-belnr.

wa_final-bukrs = wa_bseg-bukrs.

wa_final-belnr = wa_bseg-belnr.

wa_final-gjahr = wa_bseg-gjahr.

wa_final-buzei = wa_bseg-buzei.

wa_final-augbl = wa_bseg-augbl.

wa_final-bschl = wa_bseg-bschl.

wa_final-hkont = wa_bseg-hkont.

wa_final-kunnr = wa_bseg-kunnr.

wa_final-bupla = wa_bseg-bupla.

IF wa_bseg-bschl = '40'.

IF wa_bseg-koart = 'S'.

IF wa_bseg-valut ' '.

wa_final-valut = wa_bseg-valut.

wa_final-koart = wa_bseg-koart.

wa_final-zuonr = wa_bseg-zuonr.

ENDIF.

ENDIF.

ENDIF.

ENDLOOP.

LOOP AT tab_bsad INTO wa_bsad WHERE belnr = wa_bkpf_selected_data-belnr.

Check that bsad-augbl is not equal to bsad-belnr.

IF wa_bsad-augbl NE wa_bsad-belnr.

wa_final-v_invoice_no = wa_bsad-belnr.

If bsad-blart EQ 'DR'

IF wa_bsad-blart EQ c_dr.

wa_final-v_inv_amt = wa_bsad-wrbtr.

If bsad-blart EQ 'DR'

ELSEIF wa_bsad-blart NE c_dr.

wa_final-v_adv_amt = wa_bsad-wrbtr.

ENDIF.

ENDIF.

APPEND wa_final TO c_tab_final. " Please write the append statement in TAB_BSEG loop

ENDLOOP.

ENDLOOP.

0 Kudos

Hi Avinash,

Thanks for you reply.

But there are two loops inside that loop(BKPF). So shall i need to write append in those two loops. Is there any performance issue if i write like this.

Regards,

Ramana

0 Kudos

not sure exactly what u r tyring to do ..

but

LOOP AT tab_bseg INTO wa_bseg WHERE belnr = wa_bkpf_selected_data-belnr.

since this is a data retrieval u can replace this

with

SORT TAB_BESG BY BELNR.

READ TABLE TAB_BSEG WITH KEY BELNR = WA_BKPF_SELECTED_DATA-BELNR.

IF SY-SUBRC EQ 0 .

ENDIF.

 
    LOOP AT tab_bseg INTO wa_bseg WHERE belnr = wa_bkpf_selected_data-belnr.

since  this is  a data retrieval  u can replace this 

with 
  SORT TAB_BESG BY BELNR.

  READ TABLE TAB_BSEG WITH KEY BELNR = WA_BKPF_SELECTED_DATA-BELNR.

IF SY-SUBRC EQ 0 .
.....



ENDIF. 
  

In ur code try to use read statement to avoid the nested loops where ever applicable

2.

But my problem is if there are 10 items for one BELNR then all the items from BSEG was not appending to the final internal table. only the last record was appending. Can any one provide me solution to this. I am pasting the code..//

Yes it will not append unless and until you start picking the items into the target table ..

i dont see any item values in the code..

Just debug patiently with belnr and posnr values and check how this has to be moved to the target table.

0 Kudos

Hi,

Appending of the internal table should happen before the endloop,

If u have 2 different loops in your program, See how ur output table had to get populated.

For the code u have written,I suppose only the entries in ur 2nd table will get populated correctly.

U can first populate ur output table with the first table inside the loop and then modify the output tables withe the values of the 2nd table in the loop.

Check with the requirement and then proceed......

Hope this helpssss.....

JozsefSzikszai
Active Contributor
0 Kudos

you have two LOOPs inside the main LOOP (BKPF)... anyway the APPEND has to be just before the inner ENDLOOP (on bseg_tab) and probably on bsad_tab as well, but it depends on what data you have in bsad_tab and what is your exact requirement.

Former Member
0 Kudos

Hi,

For having 10 different Rows in the Final Internal table for 10 different BELNR, you need to move your APPEND Statement into an INNER LOOP...ENDLOOP.

Infact you can replace the second Loop..Endloop....with READ Statement and place your APPEND INSIDE the Loop at BSEG....Endloop.

Former Member
0 Kudos

Hi Ramana,

According to me your logic is not correct.

Reason:

Loop bkpf - Master table

Loop bseg - Child table 1

End loop u201Cbseg

Loop bsad - Child table 2

End loop u201Cbsad

Endloop u201Cbkpf

But your output is to format to 1 table output.

Point 1: Master table key u2013 1

If multiple records in child table need- Master key + child key

But you code show 1 key So I decide 1 record. Your code should be

Loop bkpf - Master table

Read bseg - Child table 1

u2026

Read bsad - Child table 2

u2026

Append

Endloop u201Cbkpf

Point 2: Ok If child has multiple record

What is the map between- 1 record in bseg with 1 record in bsad

But your output is to format to 1 table output.

Point 3: Ok If child has multiple record

Loop bkpf - 1 record

Loop bseg - loop end with last record, output struct is overwritten last value will be filled

End loop u201Cbseg

Loop bsad - loop end with last record, output struct is overwritten last value will be filled

End loop u201Cbsad

Append c_tab_final u2013 This is in right place

Endloop u201Cbkpf

So please check the requirement and you fetching method.

By

Anand