Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

Avoiding Nested Loops

Hi all,

I want to populate a internal table with Header and Item Level Details. BKPF is the Header Table and BSEG is the Item Details table. Can this be done without using nested LOOPS?

Consider the Logic I am using.

  SELECT bukrs
         belnr
         gjahr
         budat
         xblnr
         bktxt
         FROM   bkpf
         INTO TABLE t_bkpf
         WHERE      bukrs   IN p_bukrs " = p_bukrs
             AND    gjahr   IN p_gjahr  "= p_gjahr
             AND    monat   IN p_monat  " = p_monat
             AND    bstat    = space.


  IF sy-subrc IS INITIAL.

    SORT t_bkpf BY bukrs belnr gjahr.

    SELECT  bukrs
            belnr
            buzei
            gjahr
            hkont
            zlsch
            shkzg
            dmbtr
            FROM bseg
            INTO TABLE t_bseg
            FOR ALL ENTRIES IN t_bkpf
            WHERE     bukrs = t_bkpf-bukrs
               AND    gjahr = t_bkpf-gjahr
               AND    belnr = t_bkpf-belnr
               AND    hkont = w_hkont .

    IF sy-subrc IS INITIAL.

      SORT t_bseg BY  bukrs belnr gjahr buzei.
    ENDIF.

  ENDIF.


LOOP AT t_bkpf INTO wa_bkpf.
  LOOP AT t_bkpf INTO wa_bkpf WHERE bukrs = wa_bkpf-bukrs
                              AND   belnr = wa_bkpf-belnr
                              AND   gjahr = wa_bkpf-gjahr.
      wa_final-bukrs    =  wa_bkpf-bukrs.
      wa_final-budat    =  wa_bkpf-budat.
      wa_final-belnr    =  wa_bkpf-belnr.
      wa_final-shkzg    =  wa_bseg-shkzg.
      wa_final-dmbtr    =  wa_bseg-dmbtr.
      wa_final-xblnr    =  wa_bkpf-xblnr.
      wa_final-bktxt    =  wa_bkpf-bktxt.
      wa_final-hkont    =  wa_bseg-hkont.
      wa_final-zlsch    =  wa_bseg-zlsch.

   ENDLOOP.
ENDLOOP.

Thanks in advance.

Tags:
replied

Hi Arun,

This code will surely work.

I don't understand why you are not using it.

LOOP AT t_bseg INTO wa_bseg.

read table t_bkpf INTO wa_bkpf WITH key bukrs = wa_bseg-bukrs

belnr = wa_bseg-belnr

gjahr = wa_bseg-gjahr.

if sy-subrc = 0.

wa_final-bukrs = wa_bkpf-bukrs.

wa_final-budat = wa_bkpf-budat.

wa_final-belnr = wa_bkpf-belnr.

wa_final-shkzg = wa_bseg-shkzg.

wa_final-dmbtr = wa_bseg-dmbtr.

wa_final-xblnr = wa_bkpf-xblnr.

wa_final-bktxt = wa_bkpf-bktxt.

wa_final-hkont = wa_bseg-hkont.

wa_final-zlsch = wa_bseg-zlsch.

append wa_final into it_final.

clear wa_final.

endif.

ENDLOOP.

<b>IT WILL WORK</b>

REgards,

Ravi

0 View this answer in context

Helpful Answer

by
Not what you were looking for? View more on this topic or Ask a question