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 statement

Former Member
0 Likes
783

Hi all,

i m writing a report..my header table is BKDF..i m keeping a loop for BKDF table...inside the loop im writing another loop for table BKPF...the problem is i m not getting all the entries in the BKPF table...for example...

BKDF-belnr = 9100000006 it contains two entries in BKPF tabel...i want two entries to be displayed...

the code is here:

loop at gt_reptab into gw_reptab. " BKDF

loop at gt_bkpf1 into gwa_bkpf1. " bkpf

MODIFY gt_reptab FROM gw_reptab INDEX lv_tabix

TRANSPORTING belnr...

endloop.

endloop.

7 REPLIES 7
Read only

Former Member
0 Likes
727
loop at gt_reptab into gw_reptab. " BKDF

loop at gt_bkpf1 into gwa_bkpf1 where belnr 
                                        EQ gw_reptab-belnr .  
-----------
-----------
endloop.
endloop.

or do it like this:

With luck,

Pritam.

Read only

Former Member
0 Likes
727

In this case u use inside one loop u can use read statement.

Beacuse again and again u use loop that is performance issue.

Ex:

LOOP AT gt_bsid INTO gs_bsid .

READ TABLE gt_kna1 INTO gs_kna1 WITH KEY kunnr = gs_bsid-kunnr

Regards:

Prabu

Edited by: Prabu Kannappan on Aug 22, 2008 1:54 PM

Read only

0 Likes
727

the thing is when i checked in debugging it is going into loop 2 times (as my BKPF table has 2 entries)....only thing is it is not appending to my main table...that is for display....

Read only

Former Member
0 Likes
727

Hi Ravi .,

May be You are not using The FISCAL Year Field .

There may be Same Document number in the BKPF but those should be created in Different Fiscal ( Financial ) Year .

Any how pl Let us know the Exact Bussiness Requirement .

thanks

SReenivas Reddy

Read only

RaymondGiuseppi
Active Contributor
0 Likes
727

Try something like

LOOP AT gt_reptab INTO gw_reptab WHERE belnr EQ space.
  lv_tabix = sy_tabix.
  CLEAR first_occurence.
  LOOP AT gt_bkpf1 INTO gwa_bkpf1 
    WHERE bukrs = gw_reptab-bukrs 
      AND dbblg = gw_reptab-belnr.
    gw_reptab-belnr2 = gwa_bkpf1-belnr.
    IF first_occurence IS INITIAL.
       MODIFY gt_reptab FROM gw_reptab INDEX lv_tabix 
         TRANSPORTING belnr2.
       first_occurence = 'X'.
    ELSE.
      APPEND gw_reptab TO gt_reptab.
    ENDIF.
  ENDLOOP.
ENDLOOP.

Also try no to override the BELNR from BKDF with the BELNRs from BKPF

Regards

Read only

0 Likes
727

HOW CAN I WRITE APPEND STATEMENT INSIDE A LOOP...

THIS IS MY CODE.........

  • Build Internal Table

LOOP AT gt_reptab INTO gw_reptab.

lv_tabix = sy-tabix.

LOOP AT gt_detail INTO gw_detail WHERE belnr = gw_reptab-docnum

AND bukrs = gw_reptab-ccode.

*----


UHDK932263

*----Interval months: DBMON From BKDF

SELECT SINGLE DBMON INTO LV_DBMON FROM BKDF

WHERE bukrs EQ gW_reptab-ccode

AND dbbdt EQ gW_reptab-fir_run

AND BELNR EQ gw_reptab-docnum.

GW_REPTAB-DBMON = LV_DBMON.

*----FOR VENDOR FULL NAME

SELECT SINGLE NAME1 INTO LV_NAME1 FROM LFA1

WHERE LIFNR = GW_DETAIL-LIFNR.

GW_REPTAB-NAME1 = LV_NAME1.

*----


BKPF-BELNR MAY HAVE MULTIPLE ENTRIES

*--FOR BELNR IN BKPF-INVOICE DOCUMENT

  • SELECT SINGLE BELNR INTO LV_BELNR FROM BKPF

  • WHERE DBBLG EQ gw_reptab-docnum

  • AND bukrs EQ gW_reptab-ccode

  • AND GJAHR EQ gW_reptab-fyear.

*

  • GW_REPTAB-BELNR = LV_BELNR.

SELECT BELNR BUKRS GJAHR FROM BKPF INTO TABLE GT_BKPF1

WHERE DBBLG EQ gw_reptab-docnum

AND bukrs EQ gw_reptab-ccode

AND GJAHR EQ gw_reptab-fyear.

SELECT belnr BUDAT FROM BKPF INTO TABLE GT_BUDAT1

FOR ALL ENTRIES IN GT_BKPF1

WHERE BELNR EQ GT_BKPF1-BELNR

AND bukrs EQ gT_BKPF1-BUKRS

AND GJAHR EQ gT_BKPF1-GJAHR.

*----


*LOOP FOR BKPF FOR MULTIPLE ENTIRES

LOOP AT GT_BKPF1 INTO GWA_BKPF1.

GW_REPTAB-BELNR = GWA_BKPF1-BELNR.

LOOP AT GT_BUDAT1 INTO GWA_BUDAT1.

  • READ TABLE GT_BUDAT1 INTO GWA_BUDAT1

  • WITH KEY BELNR = GWA_BKPF1-BELNR.

GW_REPTAB-BUDAT = GWA_BUDAT1-BUDAT.

*---IF INVOICE NUMBER IS NULL THEN FOLLOWING ENTRIES WILL BE NULL

IF GW_REPTAB-BELNR = ' '.

MOVE ' ' TO LV_DBMON .

MOVE ' ' TO LV_NAME1.

MOVE ' ' TO LV_BELNR.

MOVE ' ' TO LV_BUDAT.

MOVE ' ' TO LV_WRBTR.

MOVE ' ' TO LV_AUGBL.

MOVE ' ' TO LV_AUGDT.

MOVE ' ' TO LV_WRBTR1.

MOVE ' ' TO LV_KOSTL.

MOVE ' ' TO LV_AUFNR.

MOVE ' ' TO LV_HKONT.

MOVE ' ' TO LV_PROJK.

ELSE.

-----DOUBT

**--FOR BUDAT IN BKPF-POSTING DATE

  • SELECT SINGLE BUDAT INTO LV_BUDAT FROM BKPF

  • WHERE BELNR EQ gw_reptab-docnum

  • AND bukrs EQ gW_reptab-ccode

  • AND GJAHR EQ gW_reptab-fyear.

*

  • GW_REPTAB-BUDAT = LV_BUDAT.

*--FOR WRBTR IN BSEG-INVOICE AMOUNT

SELECT SINGLE WRBTR INTO LV_WRBTR FROM BSEG

WHERE bukrs EQ gW_reptab-ccode

AND BELNR EQ gw_reptab-docnum

AND GJAHR EQ gW_reptab-fyear.

GW_REPTAB-WRBTR = LV_WRBTR.

*--FOR WRBTR IN BSEG-INVOICE AMOUNT

SELECT SINGLE AUGBL INTO LV_AUGBL FROM BSEG

WHERE bukrs EQ gW_reptab-ccode

AND BELNR EQ gw_reptab-docnum

AND GJAHR EQ gW_reptab-fyear.

GW_REPTAB-AUGBL = LV_AUGBL.

*--FOR WRBTR IN BSEG-INVOICE AMOUNT

SELECT SINGLE AUGDT INTO LV_AUGDT FROM BSEG

WHERE bukrs EQ gW_reptab-ccode

AND BELNR EQ gw_reptab-docnum

AND GJAHR EQ gW_reptab-fyear.

GW_REPTAB-AUGDT = LV_AUGDT.

*--FOR WRBTR IN BSEG-CLEARING AMOUNT

IF GW_REPTAB-AUGBL EQ ' '.

MOVE ' ' TO GW_REPTAB-WRBTR1.

ELSE.

SELECT SINGLE WRBTR INTO LV_WRBTR1 FROM BSEG

  • WHERE AUGBL EQ gW_reptab-AUGBL

WHERE BELNR EQ GW_REPTAB-AUGBL.

  • AND GJAHR EQ gW_reptab-fyear.

GW_REPTAB-WRBTR1 = LV_WRBTR1.

ENDIF.

*-----COST CENTRE FROM BSEG

SELECT SINGLE KOSTL INTO LV_KOSTL FROM BSEG

WHERE bukrs EQ gW_reptab-ccode

AND BELNR EQ gw_reptab-docnum

AND GJAHR EQ gW_reptab-fyear

AND SHKZG EQ 'S'.

GW_REPTAB-KOSTL = LV_KOSTL.

*-----Order FROM BSEG

SELECT SINGLE AUFNR INTO LV_AUFNR FROM BSEG

WHERE bukrs EQ gW_reptab-ccode

AND BELNR EQ gw_reptab-docnum

AND GJAHR EQ gW_reptab-fyear

AND SHKZG EQ 'S'.

GW_REPTAB-AUFNR = LV_AUFNR.

*-----G/L Account FROM BSEG

SELECT SINGLE HKONT INTO LV_HKONT FROM BSEG

WHERE bukrs EQ gW_reptab-ccode

AND BELNR EQ gw_reptab-docnum

AND GJAHR EQ gW_reptab-fyear

AND SHKZG EQ 'S'.

GW_REPTAB-HKONT1 = LV_HKONT.

*-----WBS Element FROM BSEG

SELECT SINGLE PROJK INTO LV_PROJK FROM BSEG

WHERE bukrs EQ gW_reptab-ccode

AND BELNR EQ gw_reptab-docnum

AND GJAHR EQ gW_reptab-fyear

AND SHKZG EQ 'S'.

GW_REPTAB-PROJK = LV_PROJK.

*----


  • GW_REPTAB-BUDAT = GW_DETAIL-BUDAT.

ENDIF. " BELNR IS NOT NULL

  • ENDLOOP.

*----


UHDK932263

IF gw_detail-koart = 'K'.

gw_reptab-lifnr = gw_detail-lifnr.

ELSEIF gw_detail-koart = 'S'.

gw_reptab-hkont = gw_detail-hkont.

ENDIF.

gw_reptab-ttl_pstamt = gw_detail-dmbtr * gw_reptab-docs.

ENDLOOP.

SELECT * FROM t054a INTO TABLE gt_t054a

WHERE dbakz = gw_reptab-dbakz.

DESCRIBE TABLE gt_t054a LINES length_t054a.

IF NOT ( length_t054a EQ 0 ).

gw_reptab-ttl_docamt = gw_detail-dmbtr * length_t054a.

ELSE.

CLEAR lv_moths.

CALL FUNCTION 'MONTHS_BETWEEN_TWO_DATES_NEW'

EXPORTING

i_datum_bis = gw_reptab-las_run

i_datum_von = gw_reptab-fir_run

i_kz_incl_bis = ' '

i_kz_volle_monate = 'X'

IMPORTING

e_monate = lv_moths.

gw_reptab-ttl_docamt = ( lv_moths + 1 ) * gw_detail-dmbtr.

ENDIF.

  • MODIFY GT_BKPF1 FROM GWA_BKPF1 TRANSPORTING BELNR .

*----


UHDK932263

MODIFY gt_reptab FROM gw_reptab INDEX lv_tabix

TRANSPORTING lifnr hkont ttl_docamt ttl_pstamt

BELNR BUDAT DBMON NAME1 WRBTR AUGBL AUGDT WRBTR1

KOSTL AUFNR HKONT1 PROJK.

*----


UHDK932263

CLEAR : gw_reptab, gw_detail, GWA_BKPF1, GWA_BUDAT1.

CLEAR : LV_DBMON, LV_NAME1, LV_BELNR, LV_BUDAT, LV_WRBTR, LV_AUGBL,

LV_AUGDT, LV_WRBTR1, LV_KOSTL, LV_AUFNR, LV_HKONT, LV_PROJK.

ENDLOOP.

ENDLOOP.

ENDLOOP.

Read only

0 Likes
727

First, do some clearing in the coding, this program needs a lifting

- select only once each record from database (build an internal table with all the values you need, and then loop at the data)

- perform the selection in internal tables and use them (after checking they are not empty) in FOR ALL ENTRIES clause. (As a rule try to NEVER use nested select, and no select single in a loop if the selected data may be read multiple times)

- Build the outtab only when all data have been read

- For that do in nested loops, so that you only APPEND to the final table in the last included loop.

- If big numbers of record are to be extracted, use sorted type table to LOOP WHERE

The program should look like

- SELECT primary table

- SELECT secondary tables FOR ALL ENTRIES In first itab or previous secondary tables (always check this itab is not empty)

- LOOP AT primary itab

- - LOOP AT secondary itab (if big TYPE SORTED) WHERE criteria

- - READ other secondaty itab BINARY SEATCH if one record

- - APPEND final itab

-- ENDLOOP

- ENDLOOP

- FREE itab no longuer used

Regards