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: 

looping on tables ekko ekpo and konv

Bharath84
Participant
0 Kudos

Hi All,

In my selection screen I have kschl, werks, bukrs, aedat, ebeln, ekorg as select options. I am fetching the data from ekko then ekpo and then konv based on the inputs. I have to display the data from ekko, ekpo and konv tables. Now, on which internal table I have to do the looping. Please help me.

Thanks,

Haritha

1 ACCEPTED SOLUTION

ThomasZloch
Active Contributor
0 Kudos

Use a JOIN SELECT into just one internal table, then there is no such question.


Thomas

10 REPLIES 10

karun_prabhu
Active Contributor
0 Kudos

Haritha,

     Loop that internal table which has the base records (EKKO) to fetch and append its detailed data (EKPO & KONV).

Regards.

ThomasZloch
Active Contributor
0 Kudos

Use a JOIN SELECT into just one internal table, then there is no such question.


Thomas

former_member209120
Active Contributor
0 Kudos

Hi Haritha,

Loop with EKKO and read EkPO and KNOV

EKKO -  Purchasing Document Header  (Fields EBELN, KNUMV)

EKPO - Purchasing Document Item (FIelds EBELN, EBELP)

KNOV - Conditions (Transaction Data) ( Fields KNUMV)

Loop it_ekko into wa_ekpo.

read it_ekpo into wa_ekpo with key eben = wa_ekko-ebeln

                                                             ebelp = wa_ekko-ebelp.

read it_knov into wa_knov with key KNUMV = wa_knov-knumv

endloop.

Former Member
0 Kudos

Hi,

I agree what Thomas said.

The relation between tables EKKO and EKPO is EBELN.

The relation between tables EKKO and KONV is KNUMV.

Use these relations for join statement.

Thanks & Regards,

T. Prasanna Kumar

former_member946717
Contributor
0 Kudos

Hi Haritha,

1. First select from EKKO header table with the selection screen data.

2. Then for all entries in EKKO, select from EKPO for the EBELN in EKKO table

3. Also fetch from KONV table for all entries in EKPO.

4. Loop at EKPO  so that every line item will be processed. By looping at EKKO you will miss out on processing all the line items. Hence Loop at EKPO, read EKKO if  you want any data from EKKO (else EKPO will have the EBELN with respect to EKKO itself so read on EKKO is not required)

read at KONV also within EKPO Loop.

This will help in performance tuning also. For more information you can search for abap programs in SE38 in your system or browse SDN also.

Hope this helps!

gurunathkumar_dadamu
Active Contributor
0 Kudos

Hi Haritha,

Once selecting the data from EKKO based on the selection screen,fetch the data from EKPO and KONV by using the for all entries. then loop the EKPO and read the data into final table.

Note 1 : in this is case for an EKKO we have the multiple entries in KONV,so i used the parallel cursor method to avoid the loop within the loop.

Note 2: Modify the  structures,select quires and passing the data into final tables as per your requirement add the fields.

Try the following code.

TYPE-POOLS:slis.

TABLES:ekko,ekpo,konv,t001,t001w.

*-structure for EKKO

TYPES:BEGIN OF ty_ekko,

      ebeln TYPE ekko-ebeln,

      bukrs TYPE ekko-bukrs,

      aedat TYPE ekko-aedat,

      ekorg TYPE ekko-ekorg,

      knumv TYPE ekko-knumv,

      END OF    ty_ekko.

*-struct for EKPO

TYPES:BEGIN OF ty_ekpo,

      ebeln TYPE ekpo-ebeln,

      ebelp TYPE ekpo-ebelp,

      werks TYPE ekpo-werks,

      END OF  ty_ekpo.

*-struct for KONV

TYPES:BEGIN OF ty_konv,

      knumv TYPE konv-knumv,

      kposn TYPE konv-kposn,

      kschl TYPE konv-kschl,

      kbetr TYPE konv-kbetr,

      kwert TYPE konv-kwert,

     END OF ty_konv.

*-final strucure

TYPES:BEGIN OF ty_final,

      knumv TYPE ekko-knumv,

      ebelp TYPE ekpo-ebelp,

      ebeln TYPE ekko-ebeln,

      bukrs TYPE ekko-bukrs,

      aedat TYPE ekko-aedat,

      ekorg TYPE ekko-ekorg,

      werks TYPE ekpo-werks,

      kposn TYPE konv-kposn,

      kschl TYPE konv-kschl,

      kbetr TYPE konv-kbetr,

      kwert TYPE konv-kwert,

       END OF   ty_final.

*- decalaring the itab

DATA:it_ekko TYPE STANDARD TABLE OF ty_ekko,

      it_ekpo TYPE STANDARD TABLE OF ty_ekpo,

      it_konv TYPE STANDARD TABLE OF ty_konv,

      it_final TYPE STANDARD TABLE OF ty_final,

      it_temp  TYPE STANDARD TABLE OF ty_final,

      wa_ekko TYPE ty_ekko,

      wa_ekpo TYPE ty_ekpo,

      wa_konv TYPE ty_konv,

      wa_final TYPE ty_final.

*- field catlog

DATA:it_fcat  TYPE STANDARD TABLE OF slis_fieldcat_alv,

       wa_fcat LIKE LINE OF it_fcat.

*-local varaibles.

DATA:lv_tabix TYPE sy-tabix.

*-selection-screen desgin.

SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.

SELECT-OPTIONS: s_ebeln FOR ekko-ebeln,

                 s_bukrs FOR t001-bukrs,

                 s_ekorg FOR ekko-ekorg,

                 s_aedat FOR ekko-aedat,

                 s_werks FOR t001w-werks,

                 s_kschl FOR konv-kschl.

SELECTION-SCREEN END OF BLOCK b1.

*- fetch the data.

START-OF-SELECTION.

*-from EKKO

   REFRESH : it_ekko[].

   SELECT ebeln

          bukrs

          aedat

          ekorg

          knumv

          FROM ekko

          INTO TABLE it_ekko

          WHERE ebeln IN s_ebeln

          AND   bukrs IN s_bukrs

          AND   ekorg IN s_ekorg

          AND   aedat IN s_aedat.

   IF it_ekko IS NOT INITIAL.

     REFRESH : it_ekpo[].

*-from ekpo

     SORT it_ekko BY ebeln.

     SELECT ebeln

            ebelp

            werks

            FROM ekpo

            INTO TABLE it_ekpo

            FOR ALL ENTRIES IN it_ekko

            WHERE ebeln = it_ekko-ebeln

            AND   aedat IN s_aedat

            AND   werks IN s_werks.

     IF sy-subrc = 0.

       SORT it_ekpo BY ebeln.

     ENDIF.

*-from konv

     REFRESH :it_konv[].

     SELECT knumv

            kposn

            kschl

            kbetr

            kwert

            FROM konv

            INTO TABLE it_konv

            FOR ALL ENTRIES IN it_ekko

            WHERE knumv = it_ekko-knumv

            AND kschl IN s_kschl.

     IF sy-subrc = 0.

       SORT it_konv BY knumv kposn.

     ENDIF.

   ENDIF.

*-fetching the data into final itab

   LOOP AT it_ekpo INTO wa_ekpo.

*-Read data from ekko

     READ TABLE it_ekko INTO wa_ekko WITH KEY ebeln = wa_ekpo-ebeln BINARY SEARCH.

     IF sy-subrc = 0.

       wa_final-ebeln = wa_ekko-ebeln.

       wa_final-bukrs = wa_ekko-bukrs.

       wa_final-aedat = wa_ekko-aedat.

       wa_final-ekorg = wa_ekko-ekorg.

       wa_final-knumv = wa_ekko-knumv.

       wa_final-ebelp = wa_ekpo-ebelp.

       wa_final-werks = wa_ekpo-werks.

       APPEND wa_final TO it_final.

       CLEAR:wa_ekko,

             wa_ekpo,

             wa_konv,

             wa_final.

     ENDIF.

   ENDLOOP.

   SORT it_final BY knumv ebelp.

*- use parallel cursor method.

   LOOP AT it_konv INTO wa_konv.

     READ TABLE it_final INTO wa_final WITH KEY knumv = wa_konv-knumv ebelp = wa_konv-kposn BINARY SEARCH.

     IF sy-subrc = 0.

       CLEAR:lv_tabix.

       lv_tabix = sy-tabix.

       LOOP AT it_final INTO wa_final FROM lv_tabix.

         IF wa_final-knumv <> wa_konv-knumv OR wa_final-ebelp <> wa_konv-kposn.

           EXIT.

         ELSE.

           wa_final-kposn = wa_konv-kposn.

           wa_final-kschl = wa_konv-kschl.

           wa_final-kbetr = wa_konv-kbetr.

           wa_final-kwert = wa_konv-kwert.

           APPEND wa_final TO it_temp.

           CLEAR:wa_final.

         ENDIF.

       ENDLOOP.

     ENDIF.

     CLEAR:wa_konv.

   ENDLOOP.

   REFRESH: it_final[].

   it_final[] = it_temp[].

   FREE:it_temp.

END-OF-SELECTION.

*-display  the data.

*field catlog

   PERFORM fcat.

   IF it_final IS NOT INITIAL.

     CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'

       EXPORTING

         it_fieldcat = it_fcat

       TABLES

         t_outtab    = it_final.

   ELSE.

     WRITE:'No Data'.

   ENDIF.

*&---------------------------------------------------------------------*

*&      Form  FCAT

*&---------------------------------------------------------------------*

*       text

*----------------------------------------------------------------------*

*  -->  p1        text

*  <--  p2        text

*----------------------------------------------------------------------*

FORM fcat .

   CLEAR wa_fcat.

   wa_fcat-fieldname = 'EBELN'.

   wa_fcat-tabname = 'IT_FINAL'.

   wa_fcat-seltext_m = 'Po.no'.

   APPEND wa_fcat TO it_fcat.

   CLEAR wa_fcat.

   wa_fcat-fieldname = 'EBELP'.

   wa_fcat-tabname = 'IT_FINAL'.

   wa_fcat-seltext_m = 'Po.item'.

   APPEND wa_fcat TO it_fcat.

   CLEAR wa_fcat.

   wa_fcat-fieldname = 'BUKRS'.

   wa_fcat-tabname = 'IT_FINAL'.

   wa_fcat-seltext_m = 'Comp.code'.

   APPEND wa_fcat TO it_fcat.

   CLEAR wa_fcat.

   wa_fcat-fieldname = 'AEDAT'.

   wa_fcat-tabname = 'IT_FINAL'.

   wa_fcat-seltext_m = 'Created Date'.

   APPEND wa_fcat TO it_fcat.

   CLEAR wa_fcat.

   wa_fcat-fieldname = 'EKORG'.

   wa_fcat-tabname = 'IT_FINAL'.

   wa_fcat-seltext_m = 'Po. Org'.

   APPEND wa_fcat TO it_fcat.

   CLEAR wa_fcat.

   wa_fcat-fieldname = 'WERKS'.

   wa_fcat-tabname = 'IT_FINAL'.

   wa_fcat-seltext_m = 'Plant'.

   APPEND wa_fcat TO it_fcat.

   CLEAR wa_fcat.

   wa_fcat-fieldname = 'KNUMV'.

   wa_fcat-tabname = 'IT_FINAL'.

   wa_fcat-seltext_m = 'Doc.Cond'.

   APPEND wa_fcat TO it_fcat.

   CLEAR wa_fcat.

   wa_fcat-fieldname = 'KPOSN'.

   wa_fcat-tabname = 'IT_FINAL'.

   wa_fcat-seltext_m = 'Cond.item.No'.

   APPEND wa_fcat TO it_fcat.

   CLEAR wa_fcat.

   wa_fcat-fieldname = 'KSCHL'.

   wa_fcat-tabname = 'IT_FINAL'.

   wa_fcat-seltext_m = 'Condition Type'.

   APPEND wa_fcat TO it_fcat.

   CLEAR wa_fcat.

   wa_fcat-fieldname = 'KBETR'.

   wa_fcat-tabname = 'IT_FINAL'.

   wa_fcat-seltext_m = 'Rate'.

   APPEND wa_fcat TO it_fcat.

   CLEAR wa_fcat.

   wa_fcat-fieldname = 'KWERT'.

   wa_fcat-tabname = 'IT_FINAL'.

   wa_fcat-seltext_m = 'Condition Value'.

   APPEND wa_fcat TO it_fcat.

ENDFORM.                    " FCAT

Let me know if any issues.

Regards,

Gurunath Kumar D

0 Kudos

Using a JOIN SELECT and CL_SALV_TABLE, the same can be done with 90-95% less code. In addition, it would be much better due to the access path and field catalog flexibilities.

Thomas

0 Kudos

Hi Gurunath,

Thanks for giving me the code. If I loop on table it_ekpo I am not getting all the Condition types. For each PO there are multiple Condition types. So in that case, looping on it_konv I am getting all the condition types.

Below is the code:

loop at t_konv.
  read table t_ekko with key knumv = t_konv-knumv binary search.
  if sy-subrc = 0.
    t_final-ebeln = t_ekko-ebeln.
    t_final-bukrs = t_ekko-bukrs.
    t_final-aedat = t_ekko-aedat.
    t_final-lifnr = t_ekko-lifnr.

    read table t_lfa1 with key lifnr = t_ekko-lifnr binary search.
    if sy-subrc = 0.
      t_final-name1 = t_lfa1-name1.
    endif.

    read table t_ekpo with key ebeln = t_ekko-ebeln
                               ebelp = t_konv-kposn binary search.
    if sy-subrc = 0.
      t_final-ebelp = t_ekpo-ebelp.
      t_final-matnr = t_ekpo-matnr.
      t_final-werks = t_ekpo-werks.

       t_final-knumv = t_konv-knumv.

       t_final-kposn = t_konv-kposn.

       t_final-kschl = t_konv-kschl.

       t_final-kawrt = t_konv-kawrt.

    append t_final.
    clear : t_final.
endif.
endif.

  endloop.
endif.

Please tell how to use parallel processing for the above code as I dont have loop inside loop.

Thanks,

Hatitha

0 Kudos

Hi Haritha,

   As per your requirement, loop inside the loop is one way to get the correct details.


Other way is you can build another new internal table with both ebeln and knumv by looping t_konv and reading t_ekko.

Then loop the newly build internal table(with ebeln and knumv as key) and read the t_ekpo table

Thanks,

Viji.

0 Kudos

Hi Haritha,

as per my code also am doing the same,but after fetching the data from EKKO & EKPO into final data then am looping the final table and fetching the data KONV.

Please see the my code for parallel cursor method you can understood easily.

or check the below link http://wiki.sdn.sap.com/wiki/display/Snippets/ABAP+Code+for+Parallel+Cursor+-+Loop+Processing

Regards,

Gurunath Kumar D