Is "Joins & For all entries" in same SQL Query Possible?

Hi all Professional,
Can we use "Inner Joins" and "For All Entries In" in the same SQL Query. if possible then pls clarify this query.
Here I am using three Transparent Table and fetching data from them.
SELECT abukrs abelnr ahkont axref2 ashkzg awrbtr agsber azfbdt azterm amwskz asgtxt axref1 agjahr abuzei
           bkunnr bwerks bmenge bmeins bmatnr bkoart
           cbukrs cbelnr cblart cbldat cbudat cxblnr cgjahr cstgrd cstblg cstblg c~xreversal
           INTO CORRESPONDING FIELDS OF TABLE it_bsid FROM ( ( bsid AS a
           INNER JOIN acctit AS b ON abukrs = bbukrs )
           INNER JOIN bkpf AS c ON cbukrs = abukrs
                               AND cbelnr = abelnr
                               AND cgjahr = agjahr )
           FOR ALL ENTRIES IN it_bkpf
              WHERE
                a~belnr EQ it_bkpf-belnr
            AND a~gjahr EQ it_bkpf-gjahr
            AND a~bukrs EQ it_bkpf-bukrs
            AND a~gsber IN so_bus.
After executing this query, I'm getting Dump Error.
Error analysis
    When the program was running, it was established that more
    memory was needed than the operating system is able to provide savely.
    To avoid a system crash, you must prevent this
    situation.
               Last error logged in SAP kernel
    Component............ "EM"
    Place................ "SAP-Server Development_DVL_01 on host Development (wp
     2)"
    Version.............. 37
    Error code........... 7
    Error text........... "Warning: EM-Memory exhausted: Workprocess gets PRIV "
    Description.......... " "
    System call.......... " "
    Module............... "emxx.c"
    Line................. 1886
Pls resolve, if anybody knows.
Thanks
Devinder

Hi,
During testing i notice that splitting into multiple selects does improve performance. But the best performance I achieved using DB Hints instead of splitting the select statements.
Generally performance of joins together with for all entries is bad.
However if you will look into SAP note 1662726 you will notice that this issue (bad performance in using join and for all entries together) has been addressed.
Even though the note is for HANA DB, FM RSDU_CREATE_HINT_FAE can be used independent of DB.
On HANA DB performance improvement is huge (i achieved 62 seconds using DB Hints compared to 1656 seconds using for all entries). On Oracle DB the same code initially run in 99 seconds with for all entries and with DB Hints in 82 seconds for ~ 1.000.000 records and ~660 seconds compared to 1349 seconds for ~8.000.000 records..
Sample code from SAP Note below:
Original statement:
SELECT COL1 COL2 COL3 COL4 COL5
  FROM TAB1
  INTO CORRESPONDING FIELDS OF TABLE LT_RESULT
  FOR ALL ENTRIES IN LT_SOURCE_TMP
  WHERE COL3 = LT_SOURCE_TMP-COL3
  AND   COL4 = LT_SOURCE_TMP-COL4
  AND   COL5 = LT_SOURCE_TMP-COL5
Revision:
DATA: L_T_TABLNM TYPE RSDU_T_TABLNM,
      L_LINES TYPE I,
      L_HINT TYPE RSDU_HINT.
APPEND 'TAB1' TO L_T_TABLNM.
L_LINES = LINES( LT_SOURCE_TMP ).
CALL FUNCTION 'RSDU_CREATE_HINT_FAE'
  EXPORTING
    I_T_TABLNM   = L_T_TABLNM
    I_FAE_FIELDS = 3
    I_FAE_LINES  = L_LINES
    I_EQUI_JOIN  = RS_C_TRUE
  IMPORTING
    E_HINT       = L_HINT
  EXCEPTIONS
    OTHERS       = 0.
SELECT COL1 COL2 COL3 COL4 COL5
  FROM TAB1
  INTO CORRESPONDING FIELDS OF TABLE LT_RESULT
  FOR ALL ENTRIES IN LT_SOURCE_TMP
  WHERE COL3 = LT_SOURCE_TMP-COL3
  AND   COL4 = LT_SOURCE_TMP-COL4
  AND   COL5 = LT_SOURCE_TMP-COL5
          %_HINTS ADABAS  L_HINT.
Best regards,
Octavian

Similar Messages

  • Joins & for all entries

    Hi
    1) SELECT RSEGBUKRS RSEGBELNR RSEGGJAHR RSEGBUZEI RBKPLIFNR RSEGWERKS RSEGEBELN RSEGEBELP RSEGMATNR RSEGWRBTR RBKPRBSTAT RBKPZUONR RBKPRMWWR RBKPWAERS RBKP~KURSF
    INTO TABLE I_RSEG
    FROM RBKP
    INNER JOIN RSEG
    ON
    RBKPBELNR = RSEGBELNR AND
    RBKPGJAHR = RSEGGJAHR
    FOR ALL ENTRIES IN I_BKPF
    WHERE RBKP~BELNR = I_BKPF-BELNR
    AND RBKP~GJAHR = I_BKPF-GJAHR
    AND RSEG~WERKS IN S_WERKS
    AND RBKP~LIFNR IN S_LIFNR.
    2) IF NOT I_BKPF[] IS INITIAL.
    SELECT belnr gjahr lifnr rbstat zuonr rmwwr waers kursf stblg
    FROM rbkp into corresponding fields of table i_rbkp1
    FOR ALL ENTRIES IN I_BKPF
    WHERE BELNR = I_BKPF-BELNR
    AND GJAHR = I_BKPF-GJAHR
    AND LIFNR = S_LIFNR.
    ENDIF.
    I am replacing the first select statment using inner join with for all entries. but it is not yelding same result.First select statement retrieves records while second one does not using same where conditions. How inner join would work,can we replace inner join using for all entries.
    Regards,
    Karthik.k

    Hi karthik,
    Your second query will work fine, but you made a small mistake. When using select options in the where clause of the query we should not use '=' symbol instead you shoud use IN or BETWEEN AND. your query can be written as
    IF NOT I_BKPF[] IS INITIAL.
    SELECT belnr gjahr lifnr rbstat zuonr rmwwr waers kursf stblg
    FROM rbkp into corresponding fields of table i_rbkp1
    FOR ALL ENTRIES IN I_BKPF
    WHERE BELNR = I_BKPF-BELNR
    AND GJAHR = I_BKPF-GJAHR
    AND LIFNR IN S_LIFNR.
    ENDIF.
    if values in both From and To are specified for the selection parameter S_LIFNR in selection screen, then you can use the above query or the query below
    IF NOT I_BKPF[] IS INITIAL.
    SELECT belnr gjahr lifnr rbstat zuonr rmwwr waers kursf stblg
    FROM rbkp into corresponding fields of table i_rbkp1
    FOR ALL ENTRIES IN I_BKPF
    WHERE BELNR = I_BKPF-BELNR
    AND GJAHR = I_BKPF-GJAHR
    AND ( LIFNR between S_LIFNR-LOW and S_LIFNR-HIGH ).
    ENDIF.

  • Inner Joins & for all entries

    Hi
    1) SELECT RSEGBUKRS RSEGBELNR RSEGGJAHR RSEGBUZEI RBKPLIFNR RSEGWERKS RSEGEBELN RSEGEBELP RSEGMATNR RSEGWRBTR RBKPRBSTAT RBKPZUONR RBKPRMWWR RBKPWAERS RBKP~KURSF
              INTO TABLE I_RSEG
               FROM RBKP
               INNER JOIN RSEG
               ON
               RBKPBELNR = RSEGBELNR AND
               RBKPGJAHR = RSEGGJAHR
               FOR ALL ENTRIES IN I_BKPF
               WHERE RBKP~BELNR = I_BKPF-BELNR
                 AND RBKP~GJAHR = I_BKPF-GJAHR
                 AND RSEG~WERKS IN S_WERKS
                 AND RBKP~LIFNR IN S_LIFNR.
    2) IF NOT I_BKPF[] IS INITIAL.
       SELECT belnr gjahr lifnr rbstat zuonr rmwwr waers kursf stblg
                        FROM rbkp into corresponding fields of table i_rbkp1
                           FOR ALL ENTRIES IN I_BKPF
                           WHERE BELNR = I_BKPF-BELNR
                           AND   GJAHR = I_BKPF-GJAHR
                           AND   LIFNR = S_LIFNR.
    ENDIF.
       I am replacing the first select statment using inner join with for all entries. but it is not yelding same result.First select statement retrieves records while second one does not using same where conditions. How inner join would work,can we replace inner join using for all entries.
    Regards,
    Karthik.k

    any one  suggest..
    Regards,
    Karthik

  • FOR ALL ENTRIES stmnt. in SELECT query is not running properly

    Hello experts,
              In my report program, I write one query on table BSIS.
             Internal table declaration -             
           DATA : BEGIN OF DLC_BSIS OCCURS 0,
                            BUKRS LIKE BSIS-BUKRS,
                            GJAHR LIKE BSIS-GJAHR,
                            BELNR LIKE BSIS-BELNR,  
                            SHKZG LIKE BSIS-SHKZG,
                            BSCHL LIKE BSIS-BSCHL,
                            AUFNR LIKE BSIS-AUFNR,
                            HKONT LIKE BSIS-HKONT,
                            QSSKZ LIKE BSIS-QSSKZ,
                            DMBTR LIKE BSIS-DMBTR,
                       END OF DLC_BSIS.                                                                               
    Query as follows --
             SELECT BUKRS
                           GJAHR
                           BELNR
                           AHKZG
                           BSCHL
                           AUFNR
                           HKONT
                           QSSKZ
                            DMBTR FROM BSIS
                                      INTO TABLE DLC_BSIS
                                     FOR ALL ENTRIES IN IT_BKPF2
                                     WHERE BELNR = IT_BKPF2-BELNR
                                                  AND BUKRS = IT_BKPF2-BUKRS
                                                  AND GJAHR = IT_BKPF2-GJAHR.
    IT_BKPF2 internal table having -- BUKRS - LT01
                                                         BELNR - 6400000061
                                                         GJAHR - 2009.
    And in BSIS database  table  -- 3 entries are there for the above documnet.
    But, in my internal only one entry has come for the same above document.
    I think For all entries stmnt. is not running properly. But Why it's not running properly.??
    What would be the reason..??
    Thanks in advance....!!
    Regards,
    Poonam.

    >
    Poonam Patil wrote:
    > Hello experts,
    >           In my report program, I write one query on table BSIS.
    >
    >          Internal table declaration -             
    >                                            
    >        DATA : BEGIN OF DLC_BSIS OCCURS 0,
    >                         BUKRS LIKE BSIS-BUKRS,
    >                         GJAHR LIKE BSIS-GJAHR,
    >                         BELNR LIKE BSIS-BELNR,  
    >                         SHKZG LIKE BSIS-SHKZG,
    >                         BSCHL LIKE BSIS-BSCHL,
    >                         AUFNR LIKE BSIS-AUFNR,
    >                         HKONT LIKE BSIS-HKONT,
    >                         QSSKZ LIKE BSIS-QSSKZ,
    >                         DMBTR LIKE BSIS-DMBTR,
    >                    END OF DLC_BSIS.                                                                               
    >                           
    >          Query as follows --
    >
    >          SELECT BUKRS
    >                        GJAHR
    >                        BELNR
    >                        AHKZG
    >                        BSCHL
    >                        AUFNR
    >                        HKONT
    >                        QSSKZ
    >                         DMBTR FROM BSIS
    >                                   INTO TABLE DLC_BSIS
    >                                  FOR ALL ENTRIES IN IT_BKPF2
    >                                  WHERE BELNR = IT_BKPF2-BELNR
    >                                               AND BUKRS = IT_BKPF2-BUKRS
    >                                               AND GJAHR = IT_BKPF2-GJAHR.
    >
    > IT_BKPF2 internal table having -- BUKRS - LT01
    >                                                      BELNR - 6400000061
    >                                                      GJAHR - 2009.
    >
    > And in BSIS database  table  -- 3 entries are there for the above documnet.
    >
    > But, in my internal only one entry has come for the same above document.
    >
    > I think For all entries stmnt. is not running properly. But Why it's not running properly.??
    > What would be the reason..??
    >
    >
    > Thanks in advance....!!
    >
    > Regards,
    > Poonam.
    include the buzei field in selection criteria i have faced the same situation earlier.
    always select all the key fields.
    varun

  • Inner join with for-all entries

    Why is the below inner join-for all entries does not result in expected output ?
    REPORT  ZTST3                                   .
    tables : ztst1,ztst2 .
    * table ztst1 has 4 fields : mandt,ebeln,ebelp,etenr,char4. ; ztst2 has
    * 3 fields : mandt,ebeln,ebelp,matnr
    *Entries in ztst1
    * EBELN       EBELP   ETERN  CHAR4
    * 5000000000  00010    0001    abc
    * 5000000000  00010    0002    cbd
    * 5000000000  00010    0003    efg
    *Entries in ztst2
    * EBELN       EBELP   matnr
    * 5000000000  00010    matabc
    *expected itab after inner join
    * EBELN       EBELP   CHAR4  MAtnr
    * 5000000000  00010   abc    matabc
    * 5000000000  00010   cbd    matabc
    * 5000000000  00010   efg    matabc
    data : begin of itab1 occurs 0,
           ebeln type ebeln,
           ebelp type ebelp,
           end of itab1.
    data : begin of itab occurs 0,
           ebeln type ebeln,
           ebelp type ebelp,
           char4 type char4,
           matnr type matnr,
           end of itab.
    start-of-selection.
    itab1-ebeln = '5000000000'.
    itab1-ebelp = '00010'.
    append itab1.
    select ztst1~ebeln
           ztst1~ebelp
           ztst1~char4
           ztst2~matnr into corresponding fields of table itab
    from ztst1 inner join ztst2
    on ztst1~ebeln = ztst2~ebeln and
       ztst1~ebeln = ztst2~ebelp
    for all entries in itab1
    where
    ztst1~ebeln eq itab1-ebeln and
    ztst1~ebelp eq itab1-ebelp .
    * why does it return no entries;
    break-point.

    For example in the bellow case
    *Entries in ztst1
    EBELN       EBELP   ETERN  CHAR4
    5000000000  00010    0001    abc
    5000000000  00010    0002    cbd
    5000000000  00010    0003    efg
    5000000002  00020    0003    efg
    5000000002  00020    0003    efg
    *Entries in ztst2
    EBELN       EBELP   matnr
    5000000000  00010    matabc
    5000000002  00020    abc
    may it will return you 2 records yes, than I think you have under stand the working of u201Cfor all entriesu201D ?
    And the following case I think it will work fine.
    *Entries in ztst1
    key  EBELN       EBELP   ETERN  CHAR4
    1       5000000000  00010    0001    abc
    2       5000000000  00010    0002    cbd
    3       5000000000  00010    0003    efg
    4       5000000002  00020    0003    efg
    5       5000000002  00020    0003    efg
    *Entries in ztst2
    EBELN       EBELP   matnr
    5000000000  00010    matabc
    5000000002  00020    abc
    select  ztst1~key
    ztst1~ebeln
           ztst1~ebelp
           ztst1~char4
           ztst2~matnr into corresponding fields of table itab
    from ztst1 inner join ztst2
    on ztst1~ebeln = ztst2~ebeln and
       ztst1~ebelp = ztst2~ebelp
    for all entries in itab1
    where
    ztst1~ebeln eq itab1-ebeln and
    ztst1~ebelp eq itab1-ebelp .
    Sorry, I donu2019t have system with me right now and I am sending you with out testing but I am sure that it is working the same way.
    Please Reply if any problem
    Kind Regards,
    Faisal

  • Inner join Vs For all entries

    Hi Expert,
    Could you please help me in this inner join , Inner join is performance wise is not good so i want to replace instated of inner join for all entries. But when i am checking  records of internal table there is difference. in inner join i am getting 11 records but for all entries i am getting  5 records .
    Please help me out where excatly i have done mistake.
    SELECT maralvorm maramatkl
            maramatnr marameins
            maraprdha maraspart
            marcausss marcbearz
            marcbeskz marcbstmi
            marc~wzeit                                       
            marcdisgr marcmaabc                             *         marcdismm marcdispo
            marcdispr marcdzeit
            marcplifz marcdisls
            marcbstma marcbstrf
            marc~basmg                                    
            marceisbe marcfevor
            marcfxhor marclvorm
            marcmatnr marcmmsta
            marcprctr marcsobsl
            marctranz marcwerks
            marc~xchar                                        
            marclgpro marcfhori
            marc~rgekz                                      
            mbewbklas mbewbwkey
            mbewlbkum mbewsalk3
            mbewmatnr mbewpeinh
            mbewstprs maktmaktx
            maktmatnr cepcabtei
            cepc~prctr
            marcschgt marcminbe
            marc~ekgrp
            marc~eprio                              
            marc~kausf                                        
            marc~shflg                                      
            marc~shzet                                       
            marc~fabkz
            marc~lgrad
            marc~shpro
            marc~eislo
            marc~rwpro
            marc~lgfsb
            marczzfdwe marczzfdwi                
            marczzsspe marczzsspi              
    INTO TABLE i_matl
    FROM ( mara
          INNER JOIN marc
          ON marcmatnr = maramatnr
          INNER JOIN mbew
          ON mbewmatnr = marcmatnr
          AND mbewbwkey = marcwerks
          INNER JOIN makt
          ON maktmatnr = maramatnr
          INNER JOIN cepc
          ON cepcprctr = marcprctr
          AND cepcdatbi >= sy-datum )               "*       WHERE maralvorm IN sp$00094
            AND mara~matkl IN sp$00084
            AND mara~meins IN sp$00091
            AND mara~prdha IN sp$00066
            AND mara~spart IN sp$00085
            AND marc~disgr IN sp$00089
            AND marc~dismm IN sp$00067
            AND marc~dispo IN sp$00061
            AND marc~dispr IN sp$00083
            AND marc~fevor IN sp$00090
            AND marc~lvorm IN sp$00092
            AND marc~mmsta IN sp$00086
            AND marc~beskz IN s_beskz
            AND marc~sobsl IN sp$00068
            AND marc~werks IN sp$00062
            AND marc~xchar IN sp$00093
            AND marc~maabc IN sp$00099                        
            AND marc~lgrad IN s_lgrad
            AND marc~lgpro IN s_lgpro
            AND marc~rwpro IN s_rwpro               
            AND marc~lgfsb IN s_lgfsb
             AND marc~schgt INs_schgt                          *         AND marc~shflg IN s_shflg
            AND marc~shzet IN s_shzet                
            AND marc~fabkz IN s_fabkz                    
            AND marc~shpro IN s_shpro                
            AND marc~eislo IN s_eislo               
            AND marc~zzfdwe IN s_zzfdwe                          
            AND marc~zzfdwi IN s_zzfdwi              
            AND marc~zzsspe IN s_zzsspe
            AND marc~zzsspi IN s_zzsspi
            AND mbew~bklas IN sp$00063
            AND mbew~lbkum IN sp$00088
            AND makt~maktx IN sp$00059
            AND makt~spras = c_en
            AND makt~matnr IN sp$00060
            AND cepc~prctr IN sp$00087.
    I am using for all entries
    fetching data from  marc data base table
      SELECT matnr werks lvorm  xchar mmsta
          maabc  ekgrp dispr dismm
          dispo plifz ausss disls
          beskz sobsl minbe eisbe
          bstmi bstma bstrf fhori
          rgekz fevor bearz tranz
          basmg dzeit wzeit lgrad
          prctr fxhor lgpro disgr
          kausf rwpro lgfsb schgt
          eprio shflg shzet fabkz
          shpro eislo zzfdwe zzfdwi
          zzsspe zzsspi
          FROM marc  INTO  TABLE i_marc
          WHERE  werks IN sp$00062
              AND lvorm IN sp$00092
              AND xchar IN sp$00093
              AND mmsta IN sp$00086
              AND maabc IN sp$00099
              AND dispr IN sp$00083
              AND dismm IN sp$00067
              AND dispo IN sp$00061
              AND beskz IN s_beskz
              AND sobsl IN sp$00068
              AND fevor IN sp$00090
              AND lgrad IN s_lgrad
              AND lgpro IN s_lgpro
              AND disgr IN sp$00089
              AND rwpro IN s_rwpro
              AND lgfsb IN s_lgfsb
              AND schgt IN s_schgt
              AND shflg IN s_shflg
              AND shzet IN s_shzet                
              AND fabkz IN s_fabkz                    
              AND shpro IN s_shpro                
              AND eislo IN s_eislo               
              AND zzfdwe IN s_zzfdwe                          
              AND zzfdwi IN s_zzfdwi              
              AND zzsspe IN s_zzsspe
              AND zzsspi IN s_zzsspi.
      IF sy-subrc EQ 0.
        SORT i_marc BY matnr werks.
      ENDIF.
    DELETE ADJACENT DUPLICATES FROM i_marc comparing matnr werks.
    *fetching data from mara data base table
      IF  NOT i_marc[] IS  INITIAL.
        SELECT matnr lvorm  matkl
               meins spart prdha
            FROM mara INTO TABLE i_mara
            FOR ALL ENTRIES IN i_marc
             WHERE matnr = i_marc-matnr
             AND   lvorm IN sp$00094
             AND   matkl IN sp$00084
             AND   meins IN sp$00091
             AND   prdha IN sp$00066
             AND   spart IN sp$00085.
      ENDIF.
      IF sy-subrc EQ 0.
        SORT i_mara BY matnr.
      ENDIF.
    DELETE ADJACENT DUPLICATES FROM i_mara comparing matnr.
    *fetching data from mbew data base table
      IF  NOT i_mara[] IS  INITIAL.
        SELECT matnr bwkey  bwtar lbkum
               salk3 stprs peinh bklas
               FROM mbew  INTO TABLE i_mbew
               FOR ALL ENTRIES IN i_marc
               WHERE matnr = i_marc-matnr
                    AND bwkey = i_marc-werks
                    AND bklas IN sp$00063
                    AND lbkum IN sp$00088.
      ENDIF.
      IF sy-subrc EQ 0.
        SORT i_mbew BY matnr bwkey bwtar.
      ENDIF.
    *DELETE ADJACENT DUPLICATES FROM i_mbew comparing  matnr bwkey bwtar.
    *fetching data from  makt data base  table
      IF NOT i_mara[] IS INITIAL.
        SELECT matnr spras maktx
          FROM makt INTO TABLE i_makt
          FOR ALL ENTRIES IN i_mara
          WHERE matnr = i_mara-matnr
           AND maktx IN sp$00059
           AND makt~spras = c_en.
      ENDIF.
      IF sy-subrc EQ 0.
        SORT i_makt BY matnr spras.
      ENDIF.
    DELETE ADJACENT DUPLICATES FROM i_makt.
    *fetching data from cpec data base table
      IF NOT i_marc[] IS INITIAL.
        SELECT prctr  datbi kokrs abtei
            FROM cepc INTO TABLE i_cepc
            FOR ALL ENTRIES IN i_marc
            WHERE prctr = i_marc-prctr
                 AND prctr IN sp$00087
                 AND datbi >= sy-datum.
      ENDIF.
      IF sy-subrc EQ 0.
        SORT i_cepc BY prctr datbi kokrs.
      ENDIF.
    *DELETE ADJACENT DUPLICATES FROM i_cepc.
    LOOP AT i_cepc INTO wa_cpec.
    read table i_marc into wa_marc with key
                       prctr = wa_cpec-prctr binary search.
       if sy-subrc = 0.
        i_matl-abtei  = wa_cpec-abtei.
        i_matl-prctr1 = wa_cpec-prctr.
        i_matl-ausss =  wa_marc-ausss.
        i_matl-bearz =  wa_marc-bearz.
        i_matl-beskz =  wa_marc-beskz.
        i_matl-bstmi =  wa_marc-bstmi.
        i_matl-wzeit =  wa_marc-wzeit.
        i_matl-disgr =  wa_marc-disgr.
        i_matl-maabc =  wa_marc-maabc.
        i_matl-dismm =  wa_marc-dismm.
        i_matl-dispo =  wa_marc-dispo.
        i_matl-dispr =  wa_marc-dispr.
        i_matl-dzeit =  wa_marc-dzeit.
        i_matl-plifz =  wa_marc-plifz.
        i_matl-disls =  wa_marc-disls.
        i_matl-bstma =  wa_marc-bstma.
        i_matl-bstrf =  wa_marc-bstrf.
        i_matl-basmg =  wa_marc-basmg.
        i_matl-eisbe =  wa_marc-eisbe.
        i_matl-fevor =  wa_marc-fevor.
        i_matl-fxhor =  wa_marc-fxhor.
        i_matl-lvorm1 = wa_marc-fevor.
        i_matl-matnr1 = wa_marc-matnr.
        i_matl-mmsta = wa_marc-mmsta.
        i_matl-prctr = wa_marc-prctr.
        i_matl-sobsl = wa_marc-sobsl.
        i_matl-tranz = wa_marc-tranz.
        i_matl-werks = wa_marc-werks.
        i_matl-xchar = wa_marc-xchar.
        i_matl-lgpro = wa_marc-lgpro.
        i_matl-fhori = wa_marc-fhori.
        i_matl-rgekz = wa_marc-rgekz.
        i_matl-schgt  = wa_marc-schgt.
        i_matl-minbe  = wa_marc-minbe.
        i_matl-ekgrp  = wa_marc-ekgrp.
        i_matl-eprio  = wa_marc-eprio.
        i_matl-kausf  = wa_marc-kausf.
        i_matl-shflg  = wa_marc-shflg.
        i_matl-shzet  = wa_marc-shzet.
        i_matl-fabkz  = wa_marc-fabkz.
        i_matl-lgrad  = wa_marc-lgrad.
        i_matl-shpro  = wa_marc-shpro.
        i_matl-eislo  = wa_marc-eislo.
        i_matl-rwpro  = wa_marc-rwpro.
        i_matl-lgfsb  = wa_marc-lgfsb.
        i_matl-zzfdwe = wa_marc-zzfdwe.
        i_matl-zzfdwi = wa_marc-zzfdwi.
        i_matl-zzsspe = wa_marc-zzsspe.
        i_matl-zzsspi = wa_marc-zzsspi.
    endif.
        READ  TABLE i_mara INTO wa_mara WITH KEY
                            matnr = wa_marc-matnr  binary search.
        i_matl-lvorm =  wa_mara-lvorm.
        i_matl-matkl =  wa_mara-matkl.
        i_matl-matnr =  wa_mara-matnr.
        i_matl-meins =  wa_mara-meins.
        i_matl-prdha =  wa_mara-prdha.
        i_matl-spart =  wa_mara-spart.
        READ TABLE  i_makt INTO wa_makt WITH KEY
                            matnr = wa_mara-matnr binary search.
        i_matl-matnr3 = wa_makt-matnr.
        i_matl-maktx = wa_makt-maktx.
        READ TABLE i_mbew INTO wa_mbew WITH  KEY
                              matnr = wa_marc-matnr binary search.
        i_matl-bklas = wa_mbew-bklas.
        i_matl-bwkey = wa_mbew-bwkey.
        i_matl-lbkum = wa_mbew-lbkum.
        i_matl-salk3 = wa_mbew-salk3.
        i_matl-matnr2 = wa_mbew-matnr.
        i_matl-peinh  = wa_mbew-peinh.
        i_matl-stprs = wa_mbew-stprs.
       READ TABLE i_cepc INTO wa_cpec WITH KEY
                             prctr = wa_marc-prctr.
        APPEND  i_matl.
        CLEAR: wa_cpec, wa_mbew, wa_mara, wa_marc.
      ENDLOOP.
    Thaks
    Waiting your reply.

    Hi Sasmita,
    I think your final loop is the problem. As you are looping through the CEPC internal table, and reading the other tables. For different materials there might be a same profit center, the read table will fetch you only first occurance of the profit center from the mara or marc table.
    So loop through either mara or marc table and read the remaining and populate your final internal tables. This might solve your problem.
    Regards,
    Ramesh Babu S
    *Reward points if it is helpful....

  • For all entries or join

    Hi,
    please which should be used between for all entries or join????

    All abap programers and most of the dba's that support abap programmers are familiar with the abap clause "for all entries". Most of the web pages I visited recently, discuss 3 major drawbacks of the "for all entries" clause:
    1. duplicate rows are automatically removed
    2. if the itab used in the clause is empty , all the rows in the source table will be selected .
    3. performance degradation when using the clause on big tables.
    In this post I'd like to shed some light on the third issue. Specifically i'll discuss the use of the "for all entries" clause as a means to join tables in the abap code instead of in db2.
    Say for example you have the following abap code:
    Select * from mara
    For all entries in itab
    Where matnr = itab-matnr.
    If the actual source of the material list (represented here by itab) is actually another database table, like:
    select matnr from mseg
    into corresponding fields of table itab
    where �.
    Then you could have used one sql statement that joins both tables.
    Select t1.*
    From mara t1, mseg t2
    Where t1.matnr = t2.matnr
    And T2�..
    So what are the drawbacks of using the "for all entires" instead of a join ?
    At run time , in order to fulfill the "for all entries " request, the abap engine will generate several sql statements (for detailed information on this refer to note 48230). Regardless of which method the engine uses (union all, "or" or "in" predicates) If the itab is bigger then a few records, the abap engine will break the itab into parts, and rerun an sql statement several times in a loop. This rerun of the same sql statement , each time with different host values, is a source of resource waste because it may lead to re-reading of data pages.
    returing to the above example , lets say that our itab contains 500 records and that the abap engine will be forced to run the following sql statement 50 times with a list of 10 values each time.
    Select * from mara
    Where matnr in ( ...)
    Db2 will be able to perform this sql statement cheaply all 50 times, using one of sap standard indexes that contain the matnr column. But in actuality, if you consider the wider picture (all 50 executions of the statement), you will see that some of the data pages, especially the root and middle-tire index pages have been re-read each execution.
    Even though db2 has mechanisms like buffer pools and sequential detection to try to minimize the i/o cost of such cases, those mechanisms can only minimize the actual i/o operations , not the cpu cost of re-reading them once they are in memory. Had you coded the join, db2 would have known that you actually need 500 rows from mara, it would have been able to use other access methods, and potentially consume less getpages i/o and cpu.
    In other words , when you use the "for all entries " clause instead of coding a join , you are depriving the database of important information needed to select the best access path for your application. Moreover, you are depriving your DBA of the same vital information. When the DBA monitors & tunes the system, he (or she) is less likely to recognize this kind of resource waste. The DBA will see a simple statement that uses an index , he is less likely to realize that this statement is executed in a loop unnecessarily.
    In conclusion I suggest to "think twice" before using the "for all entries" clause and to evaluate the use of database views as a means to:
    a. simplify sql
    b. simplify abap code
    c. get around open sql limitations.

  • Replacing a inner join with for all entries

    Hi Team,
       In a already developed program I am replacing a inner join with select query follow up with for-all-entris and passing the data to final internal table but in both the case the result should be same then only my replacement will be correct. But my no records in both cases differs. This happening because when i am selecting data from first data base table is 32 lines. then I am doing fo-all-entries moving all the duplicate entries then the no records are four. but in final internal table i am looping the first internal table. So in final internal table the no of records are 32. But in inner join query the records are 16.So please let me know how resolve this issue?
    Thanks and REgards
    Deepa

    Hi Thomas,
      Thanks for ur suggestion.
    The solved that in below.
    In select query I did not change anything The way I had written the code was correct.
    I think many of us know how to write that how to make the performance better in that way.
    I made the change when I transfered the to final internal table.
    The original Inner join code:
    select a~field1 a~field2 a~field3 b~field2 b~field3 b~field4
               from dbtab1 as a  inner join dbtab2 as b
              on a~field1 = b~field1 into it_final where
              a~field1 in s_field1. [Field1  in both the table are key field]
    Before code:
    Sort itab1 by key-fields.
    sort itab2 by keyfields.
    loop at itab1 into wa1.
    move: wa1-field1 to wa_final-field1,
               wa1-field2 to wa_final-field2,
               wa1-field3 to wa_final-field3.
    read table itab2 into wa2 witk key field1 = wa1-field1 binary search.
      if sy-subrc = 0.
      move : wa2-field2 to wa_final-field4,
                 wa2-field3 to wa_final-field5,
                 wa2-field4 to wa_final-field6.
    append wa_final to it_final.
    endif.
    Clear : wa1, wa2, wa_final.
    endloop.
    In this case if the one key fieild value is not present there in second internal table but its there in first internal table still it will read that row with 2nd internal values having zeroes. Normally what does not happen in inner join case if the key field value will same in both the case ,then that will fetch only those rows.
    Changed Code
    loop at itab1 into wa1.
    read table itab2 into wa2 witk key field1 = wa1-field1 binary search.
      if sy-subrc = 0.
    move: wa1-field1 to wa_final-field1,
               wa1-field2 to wa_final-field2,
               wa1-field3 to wa_final-field3.
      move : wa2-field2 to wa_final-field4,
                 wa2-field3 to wa_final-field5,
                 wa2-field4 to wa_final-field6.
    append wa_final to it_final.
    endif.
    Clear : wa1, wa2, wa_final.
    endloop.
    In this case the values will read to final internal table if both key field matches.
    With Regards
    Deepa

  • Does 'For All Entries in itab' work exactly like 'Join' statement?

    Hi,
    I would like to know that if 'For All Entries in itab' work exactly like 'Join' statement?
    If yes, then when I use 'For All Entries in itab' and a 'Join' statement seperately with the same logical conditions for both, the number of records returned by the two methods are not same. Ideally, they should both return the same number of recs.
    Can somebody help?
    With regards.

    Hi,
    for all entries will not work in the same way unless untill it should satisfy some conditions,
    it has some pre-requisests...
    like in the select clause or in where clause or in both the cluases, there should be entire key..
    then only it will behave like the join statement..
    hope i am clear.
    please revert back if u have any quiries.
    Regards,
    Sunil Kumar Mutyala.

  • Hi guru's what is the diff between for all entries & joins

    hi guru's what is the diff between for all entries & joins

    Hi Vasu,
    Joins are used to fetch data fast from Database tables:
    Tables are joined with the proper key fields to fetch the data properly.
    If there are no proper key fields between tables don't use Joins;
    Important thing is that don't USE JOINS FOR CLUSTER tableslike BSEG and KONV.
    Only use for Transparenmt tables.
    You can also use joins for the database VIews to fetch the data.
    JOINS
    ... FROM tabref1 [INNER] JOIN tabref2 ON cond
    Effect
    The data is to be selected from transparent database tables and/or views determined by tabref1 and tabref2. tabref1 and tabref2 each have the same form as in variant 1 or are themselves Join expressions. The keyword INNER does not have to be specified. The database tables or views determined by tabref1 and tabref2 must be recognized by the ABAP Dictionary.
    In a relational data structure, it is quite normal for data that belongs together to be split up across several tables to help the process of standardization (see relational databases). To regroup this information into a database query, you can link tables using the join command. This formulates conditions for the columns in the tables involved. The inner join contains all combinations of lines from the database table determined by tabref1 with lines from the table determined by tabref2, whose values together meet the logical condition (join condition) specified using ON>cond.
    Inner join between table 1 and table 2, where column D in both tables in the join condition is set the same:
    Table 1 Table 2
    A
    B
    C
    D
    D
    E
    F
    G
    H
    a1
    b1
    c1
    1
    1
    e1
    f1
    g1
    h1
    a2
    b2
    c2
    1
    3
    e2
    f2
    g2
    h2
    a3
    b3
    c3
    2
    4
    e3
    f3
    g3
    h3
    a4
    b4
    c4
    3
    |--|||--|
    Inner Join
    A
    B
    C
    D
    D
    E
    F
    G
    H
    a1
    b1
    c1
    1
    1
    e1
    f1
    g1
    h1
    a2
    b2
    c2
    1
    1
    e1
    f1
    g1
    h1
    a4
    b4
    c4
    3
    3
    e2
    f2
    g2
    h2
    |--||||||||--|
    Example
    Output a list of all flights from Frankfurt to New York between September 10th and 20th, 2001 that are not sold out:
    DATA: DATE LIKE SFLIGHT-FLDATE,
    CARRID LIKE SFLIGHT-CARRID,
    CONNID LIKE SFLIGHT-CONNID.
    SELECT FCARRID FCONNID F~FLDATE
    INTO (CARRID, CONNID, DATE)
    FROM SFLIGHT AS F INNER JOIN SPFLI AS P
    ON FCARRID = PCARRID AND
    FCONNID = PCONNID
    WHERE P~CITYFROM = 'FRANKFURT'
    AND P~CITYTO = 'NEW YORK'
    AND F~FLDATE BETWEEN '20010910' AND '20010920'
    AND FSEATSOCC < FSEATSMAX.
    WRITE: / DATE, CARRID, CONNID.
    ENDSELECT.
    If there are columns with the same name in both tables, you must distinguish between them by prefixing the field descriptor with the table name or a table alias.
    Note
    In order to determine the result of a SELECT command where the FROM clause contains a join, the database system first creates a temporary table containing the lines that meet the ON condition. The WHERE condition is then applied to the temporary table. It does not matter in an inner join whether the condition is in the ON or WHEREclause. The following example returns the same solution as the previous one.
    Example
    Output of a list of all flights from Frankfurt to New York between September 10th and 20th, 2001 that are not sold out:
    DATA: DATE LIKE SFLIGHT-FLDATE,
    CARRID LIKE SFLIGHT-CARRID,
    CONNID LIKE SFLIGHT-CONNID.
    SELECT FCARRID FCONNID F~FLDATE
    INTO (CARRID, CONNID, DATE)
    FROM SFLIGHT AS F INNER JOIN SPFLI AS P
    ON FCARRID = PCARRID
    WHERE FCONNID = PCONNID
    AND P~CITYFROM = 'FRANKFURT'
    AND P~CITYTO = 'NEW YORK'
    AND F~FLDATE BETWEEN '20010910' AND '20010920'
    AND FSEATSOCC < FSEATSMAX.
    WRITE: / DATE, CARRID, CONNID.
    ENDSELECT.
    Note
    Since not all of the database systems supported by SAP use the standard syntax for ON conditions, the syntax has been restricted. It only allows those joins that produce the same results on all of the supported database systems:
    Only a table or view may appear to the right of the JOIN operator, not another join expression.
    Only AND is possible in the ON condition as a logical operator.
    Each comparison in the ON condition must contain a field from the right-hand table.
    If an outer join occurs in the FROM clause, all the ON conditions must contain at least one "real" JOIN condition (a condition that contains a field from tabref1 amd a field from tabref2.
    Note
    In some cases, '*' may be specified in the SELECT clause, and an internal table or work area is entered into the INTO clause (instead of a list of fields). If so, the fields are written to the target area from left to right in the order in which the tables appear in the FROM clause, according to the structure of each table work area. There can then be gaps between table work areas if you use an Alignment Request. For this reason, you should define the target work area with reference to the types of the database tables, not simply by counting the total number of fields. For an example, see below:
    Variant 3
    ... FROM tabref1 LEFT [OUTER] JOIN tabref2 ON cond
    Effect
    Selects the data from the transparent database tables and/or views specified in tabref1 and tabref2. tabref1 und tabref2 both have either the same form as in variant 1 or are themselves join expressions. The keyword OUTER can be omitted. The database tables or views specified in tabref1 and tabref2 must be recognized by the ABAP-Dictionary.
    In order to determine the result of a SELECT command where the FROM clause contains a left outer join, the database system creates a temporary table containing the lines that meet the ON condition. The remaining fields from the left-hand table (tabref1) are then added to this table, and their corresponding fields from the right-hand table are filled with ZERO values. The system then applies the WHERE condition to the table.
    Left outer join between table 1 and table 2 where column D in both tables set the join condition:
    Table 1 Table 2
    A
    B
    C
    D
    D
    E
    F
    G
    H
    a1
    b1
    c1
    1
    1
    e1
    f1
    g1
    h1
    a2
    b2
    c2
    1
    3
    e2
    f2
    g2
    h2
    a3
    b3
    c3
    2
    4
    e3
    f3
    g3
    h3
    a4
    b4
    c4
    3
    |--|||--|
    Left Outer Join
    A
    B
    C
    D
    D
    E
    F
    G
    H
    a1
    b1
    c1
    1
    1
    e1
    f1
    g1
    h1
    a2
    b2
    c2
    1
    1
    e1
    f1
    g1
    h1
    a3
    b3
    c3
    2
    NULL
    NULL
    NULL
    NULL
    NULL
    a4
    b4
    c4
    3
    3
    e2
    f2
    g2
    h2
    |--||||||||--|
    Example
    Output a list of all custimers with their bookings for October 15th, 2001:
    DATA: CUSTOMER TYPE SCUSTOM,
    BOOKING TYPE SBOOK.
    SELECT SCUSTOMNAME SCUSTOMPOSTCODE SCUSTOM~CITY
    SBOOKFLDATE SBOOKCARRID SBOOKCONNID SBOOKBOOKID
    INTO (CUSTOMER-NAME, CUSTOMER-POSTCODE, CUSTOMER-CITY,
    BOOKING-FLDATE, BOOKING-CARRID, BOOKING-CONNID,
    BOOKING-BOOKID)
    FROM SCUSTOM LEFT OUTER JOIN SBOOK
    ON SCUSTOMID = SBOOKCUSTOMID AND
    SBOOK~FLDATE = '20011015'
    ORDER BY SCUSTOMNAME SBOOKFLDATE.
    WRITE: / CUSTOMER-NAME, CUSTOMER-POSTCODE, CUSTOMER-CITY,
    BOOKING-FLDATE, BOOKING-CARRID, BOOKING-CONNID,
    BOOKING-BOOKID.
    ENDSELECT.
    If there are columns with the same name in both tables, you must distinguish between them by prefixing the field descriptor with the table name or using an alias.
    Note
    For the resulting set of a SELECT command with a left outer join in the FROM clause, it is generally of crucial importance whether a logical condition is in the ON or WHERE condition. Since not all of the database systems supported by SAP themselves support the standard syntax and semantics of the left outer join, the syntax has been restricted to those cases that return the same solution in all database systems:
    Only a table or view may come after the JOIN operator, not another join statement.
    The only logical operator allowed in the ON condition is AND.
    Each comparison in the ON condition must contain a field from the right-hand table.
    Comparisons in the WHERE condition must not contain a field from the right-hand table.
    The ON condition must contain at least one "real" JOIN condition (a condition in which a field from tabref1 as well as from tabref2 occurs).
    Note
    In some cases, '*' may be specivied as the field list in the SELECT clause, and an internal table or work area is entered in the INTO clause (instead of a list of fields). If so, the fields are written to the target area from left to right in the order in which the tables appear in the llen in der FROM clause, according to the structure of each table work area. There can be gaps between the table work areas if you use an Alignment Request. For this reason, you should define the target work area with reference to the types of the database tables, as in the following example (not simply by counting the total number of fields).
    Example
    Example of a JOIN with more than two tables: Select all flights from Frankfurt to New York between September 10th and 20th, 2001 where there are available places, and display the name of the airline.
    DATA: BEGIN OF WA,
    FLIGHT TYPE SFLIGHT,
    PFLI TYPE SPFLI,
    CARR TYPE SCARR,
    END OF WA.
    SELECT * INTO WA
    FROM ( SFLIGHT AS F INNER JOIN SPFLI AS P
    ON FCARRID = PCARRID AND
    FCONNID = PCONNID )
    INNER JOIN SCARR AS C
    ON FCARRID = CCARRID
    WHERE P~CITYFROM = 'FRANKFURT'
    AND P~CITYTO = 'NEW YORK'
    AND F~FLDATE BETWEEN '20010910' AND '20010920'
    AND FSEATSOCC < FSEATSMAX.
    WRITE: / WA-CARR-CARRNAME, WA-FLIGHT-FLDATE, WA-FLIGHT-CARRID,
    WA-FLIGHT-CONNID.
    ENDSELECT.
    And for all entries,
    this will help u.
    use of FOR ALL ENTRIES:
    1. INNER JOIN
    DBTAB1 <----
    > DBTAB2
    It is used to JOIN two DATABASE tables
    having some COMMON fields.
    2. Whereas
    For All Entries,
    DBTAB1 <----
    > ITAB1
    is not at all related to two DATABASE tables.
    It is related to INTERNAL table.
    3. If we want to fetch data
    from some DBTABLE1
    but we want to fetch
    for only some records
    which are contained in some internal table,
    then we use for alll entries.
    1. simple example of for all entries.
    2. NOTE THAT
    In for all entries,
    it is NOT necessary to use TWO DBTABLES.
    (as against JOIN)
    3. use this program (just copy paste)
    it will fetch data
    from T001
    FOR ONLY TWO COMPANIES (as mentioned in itab)
    4
    REPORT abc.
    DATA : BEGIN OF itab OCCURS 0,
    bukrs LIKE t001-bukrs,
    END OF itab.
    DATA : t001 LIKE TABLE OF t001 WITH HEADER LINE.
    itab-bukrs = '1000'.
    APPEND itab.
    itab-bukrs = '1100'.
    APPEND itab.
    SELECT * FROM t001
    INTO TABLE t001
    FOR ALL ENTRIES IN itab
    WHERE bukrs = itab-bukrs.
    LOOP AT t001.
    WRITE :/ t001-bukrs.
    ENDLOOP.
    cheers,
    Hema.

  • Plz change code from inner join to for all entries

    REPORT      : ZSD00009                                               *
    DESCRIPTION : REPORT FOR TRACTOR ON STOCK AGING                      *
    CODED BY    : DINESH AGARWAL                                         *
    SPECS BY    : AJAY KOTHI                                             *
    START DATE  : 07TH JUNE , 2000                                       *
    CHANGED     : MANOJ SINGH 01/02/2001                                 *
    CHANGED     : YOGINDER KAURA 27TH JULY 2001                          *
    REPORT  ZMUSA_SD00009                           .
    TABLES : vbrk,vbrp,vbfa,ser01,objk,knvv,mara,mbew, t005u,
             t171t,kna1,equi,ihpa, cabn, ausp.
    DATA : BEGIN OF itab OCCURS 0,
             bzirk1 LIKE vbrk-bzirk,
             bzirk LIKE KNA1-regio, "added by dhiraj - 23.11.2007
             LAND1 LIKE KNA1-LAND1,
             kunde LIKE equi-kunde,
             vbeln LIKE vbrk-vbeln,
             fkdat LIKE vbrk-fkdat,
             matkl LIKE mara-matkl,
             sernr LIKE objk-sernr,
             equnr LIKE objk-equnr ,
             matnr LIKE vbrp-matnr,
             spart LIKE vbrp-spart,
             vbelv LIKE vbrp-vbelv,
             posnv LIKE vbrp-posnv,
             days1(1) TYPE n ,
             days2(1) TYPE n ,
             days3(1) TYPE n ,
             days4(1) TYPE n ,
             days5(1) TYPE n ,
             werks like vbrp-werks,
           END OF itab.
    DATA : BEGIN OF itab_grp OCCURS 0,
             werks like vbrp-werks,
             matkl LIKE mara-matkl,
             bzirk1 LIKE vbrk-bzirk,
             bzirk LIKE KNA1-regio, "added by dhiraj - 23.11.2007
             kunde LIKE equi-kunde,
             vbeln LIKE vbrk-vbeln,
             fkdat LIKE vbrk-fkdat,
             sernr LIKE objk-sernr,
             equnr LIKE objk-equnr ,
             matnr LIKE vbrp-matnr,
             spart LIKE vbrp-spart,
             vbelv LIKE vbrp-vbelv,
             posnv LIKE vbrp-posnv,
             days1(1) TYPE n ,
             days2(1) TYPE n ,
             days3(1) TYPE n ,
             days4(1) TYPE n ,
             days5(1) TYPE n ,
           END OF itab_grp.
    DATA : BEGIN OF dealer ,
           days1(3) TYPE p ,
           days2(3) TYPE p ,
           days3(3) TYPE p ,
           days4(3) TYPE p ,
           days5(3) TYPE p ,
           END OF dealer .
    DATA : state LIKE dealer .
    DATA : matgr LIKE dealer .
    DATA : plant LIKE dealer .
    DATA :  BEGIN OF us,
           days1(4) TYPE p ,
           days2(4) TYPE p ,
           days3(4) TYPE p ,
           days4(4) TYPE p ,
           days5(4) TYPE p ,
           END OF us .
    DATA : lin TYPE i , check_box(1) ,
           dealer_total TYPE i,state_total  TYPE i,
           matgr_total  TYPE i,plant_total  TYPE i,
           us_total TYPE i .
    DATA  vobjnr(22) .
    DATA  no_of_days TYPE i .
    DATA  flag_fkdat.
    DATA  atinn1 LIKE cabn-atinn.
    DATA  atinn LIKE cabn-atinn.
    DATA : atflv LIKE ausp-atflv.
    DATA : atflv1(8) TYPE n.
    DATA : invdate TYPE sy-datum.
    DATA  invoicenum(10) TYPE n .
    DATA : BEGIN OF it_regio OCCURS 0,
            regio LIKE kna1-regio,
           END OF it_regio.
    data : it001w like table of t001w with header line.
    TABLES  vbpa.
    SELECTION-SCREEN BEGIN OF BLOCK block1 WITH FRAME TITLE text-001.
    SELECT-OPTIONS : werks FOR vbrp-werks,
                     vkorg FOR vbrk-vkorg,
                     vtweg FOR vbrk-vtweg,
                     spart FOR vbrp-spart.
    SELECTION-SCREEN END OF BLOCK block1 .
    SELECTION-SCREEN SKIP.
    SELECTION-SCREEN BEGIN OF BLOCK block2 WITH FRAME TITLE text-002.
    SELECT-OPTIONS : matkl FOR mara-matkl,
                     bzirk FOR vbrk-regio," changed by dhiraj - 23.11.2007
                     kunrg FOR vbrk-kunrg ,
                     fkdat FOR vbrk-fkdat.
    SELECTION-SCREEN END OF BLOCK block2 .
    SELECTION-SCREEN BEGIN OF BLOCK block3 WITH FRAME TITLE text-003.
    PARAMETERS : days1(2) TYPE p OBLIGATORY DEFAULT '90' ,
                 days2(2) TYPE p OBLIGATORY DEFAULT '90',
                 days3(2) TYPE p OBLIGATORY DEFAULT '180',
                 days4(2) TYPE p OBLIGATORY DEFAULT '270',
                 days5(2) TYPE p OBLIGATORY DEFAULT '365'.
    SELECTION-SCREEN END OF BLOCK block3 .
    SELECTION-SCREEN BEGIN OF BLOCK reptype WITH FRAME TITLE text-201 .
    PARAMETERS  : detail RADIOBUTTON GROUP rep ,
                  summary RADIOBUTTON GROUP rep ,
                  grp_summ RADIOBUTTON GROUP rep .
    SELECTION-SCREEN END OF BLOCK reptype .
    *start of code by Kalpana for Authority check for REGIO */
    AT SELECTION-SCREEN .
      SELECT regio FROM kna1 INTO TABLE it_regio
       WHERE regio IN bzirk
       AND   kunnr IN kunrg.
      CLEAR it_regio.
      LOOP AT it_regio.
        AUTHORITY-CHECK OBJECT 'Z:AO_REGIO'
                         ID 'REGIO' FIELD it_regio.
        IF sy-subrc NE 0.
          MESSAGE e173(zma) WITH  it_regio.
        ENDIF.
      ENDLOOP.
    *End of code by Kalpana for Authority check for REGIO */
      IF days1 NE days2 .
        MESSAGE e000(8i) WITH text-004  text-009  text-005.
      ELSEIF days3 LE days2 .
        MESSAGE e000(8i) WITH  text-006  text-010  text-005 .
      ELSEIF days4 LE days3 .
        MESSAGE e000(8i) WITH  text-007  text-010  text-006 .
      ELSEIF days5 LE days4 .
        MESSAGE e000(8i) WITH  text-008  text-010  text-007 .
      ENDIF .
    START-OF-SELECTION .
    select abzirk akunrg avbeln afkdat  b~matnr
            cmatkl dvbelv dposnv fsernr  f~equnr
      into corresponding fields of table itab
      from    ( ( ( ( ( vbrk as a inner join vbrp as b
                                              on avbeln = bvbeln )
                           inner join mara as c on cmatnr = bmatnr )
                           inner join vbfa as d on dvbeln = bvbeln
                                               and dposnn = bposnr )
                           inner join ser01 as e on elief_nr = dvbelv
                                                 and eposnr   = dposnv )
                           inner join objk as f on fobknr = eobknr   )
             where a~vkorg in  vkorg
             and   a~vtweg in  vtweg
             and   a~bzirk in  bzirk
             and   a~fkdat in  fkdat
             and   c~matkl in  matkl
             and   a~kunrg in  kunrg
             and   b~spart in  spart
             and   c~mtart = 'FERT'
             and   d~vbtyp_n =  'M'
             and   a~vbtyp   =  'M'
             and   a~fksto   =  ' '
             and   b~vgtyp   =  'J' .
      REFRESH itab.
      CLEAR itab .
      CLEAR cabn .
      select * from t001w into table it001w where werks in werks.
      SELECT  SINGLE  atinn INTO atinn1 FROM  cabn
             WHERE  atnam  = 'INVOICEDATE'.
    *************************modified by dhiraj - 23.11.2007*****************
    SELECT  kbzirk aatflv ekunde eequnr esernr ematnr m~matkl
             INTO CORRESPONDING FIELDS OF  itab
            FROM ( ( ( equi AS e  INNER JOIN ausp AS a
                 ON eequnr = aobjek  )
                  INNER JOIN mara AS m ON ematnr  = mmatnr )
                  INNER JOIN knvv AS k ON kkunnr  = ekunde
                                       AND kspart = mspart )
                          WHERE m~mtart = 'FERT'
                          AND   m~spart IN spart
                          AND   m~matkl IN matkl
                          AND  e~kunde <> ' '
                          AND  e~kunde IN kunrg
                          AND  k~bzirk IN  bzirk
                          and  a~atinn = atinn1
                          AND  k~vkorg = 'M001'
                          AND  k~vtweg = '90'.
      SELECT  kbzirk nregio nland1 aatflv ekunde eequnr esernr ematnr m~matkl
              INTO CORRESPONDING FIELDS OF itab
             FROM ( ( ( ( equi AS e  INNER JOIN ausp AS a
                  ON eequnr = aobjek  )
                   INNER JOIN mara AS m ON ematnr  = mmatnr )
                   INNER JOIN knvv AS k ON kkunnr  = ekunde
                                        AND kspart = mspart )
                   INNER JOIN kna1 as n ON nkunnr  = kkunnr )
                           WHERE m~mtart = 'FERT'
                           AND   m~spart IN spart
                           AND   m~matkl IN matkl
                           AND  e~kunde <> ' '
                           AND  e~kunde IN kunrg
                           AND  nregio IN bzirk "k-bzirk changed to nregio by dhiraj-23.11.2007
                          and  a~atinn = atinn1
                           AND  k~vkorg = 'F041'
                           AND  k~vtweg = '01'.
       added by krunal for plant as select-option
        clear ausp.
        select single * from cabn where atnam = 'DELIVERINGPLANT'.
        SELECT  SINGLE      * FROM  ausp
               WHERE  objek  = itab-equnr
               AND    atinn  = CABN-ATINN.
       CLEAR CABN.
        read table it001w with key werks = ausp-atwrt.
        if sy-subrc <> 0.
         continue.
        else.
          itab-werks = ausp-atwrt.
        endif.
    *end addition
        CLEAR : ausp, knvv .
        SELECT  SINGLE      * FROM  ausp
               WHERE  objek  = itab-equnr
               AND    atinn  = atinn1 .
        MOVE ausp-atflv   TO atflv1 .
        MOVE atflv1 TO invdate .
        MOVE invdate TO itab-fkdat.
        CLEAR : cabn, ausp, atinn.
        SELECT  SINGLE  atinn INTO atinn FROM  cabn
               WHERE  atnam  = 'INVOICENO' .
        SELECT SINGLE      * FROM  ausp
             WHERE  objek  = itab-equnr
             AND    atinn  = atinn.
        MOVE ausp-atflv TO invoicenum .
        MOVE invoicenum TO itab-vbeln.
        IF NOT fkdat-low IS INITIAL .
          IF invdate GE fkdat-low AND invdate LE fkdat-high .
            APPEND itab.
          ELSE.
            CONTINUE.
          ENDIF.
        ELSE.
          APPEND itab.
        ENDIF.
      ENDSELECT.
      SORT itab BY sernr matnr .
      DELETE ADJACENT DUPLICATES FROM itab COMPARING sernr matnr .
      SORT itab BY equnr .
      LOOP AT itab .
        CONCATENATE 'IE' itab-equnr INTO vobjnr .
        SELECT SINGLE * FROM ihpa  WHERE objnr = vobjnr
                                 AND   parvw = 'RE'
                                 AND   obtyp = 'IEQ'
                                 AND kzloesch EQ space. " Yogi on 27/06/2001
        IF sy-subrc EQ 0 .
          DELETE itab INDEX sy-tabix .
          CONTINUE .
        ELSE .
          no_of_days = sy-datum - itab-fkdat .
          IF no_of_days LE days1 .
            itab-days1 = 1 .
          ELSEIF no_of_days GT days2 AND no_of_days LE days3.
            itab-days2 = 1 .
          ELSEIF no_of_days GT days3 AND no_of_days LE days4.
            itab-days3 = 1 .
          ELSEIF no_of_days GT days4 AND no_of_days LE days5.
            itab-days4 = 1 .
          ELSEIF no_of_days GT days5 .
            itab-days5 = 1 .
          ENDIF .
          MODIFY itab INDEX sy-tabix .
        ENDIF .
      ENDLOOP .
    END-OF-SELECTION .
      DESCRIBE TABLE itab LINES lin .
      IF lin GT 0 .
        SORT itab BY bzirk kunde  vbeln .
        IF detail = 'X' .
          PERFORM disp_list .
        ELSEIF summary = 'X' .
          PERFORM summary_list .
        ELSEIF grp_summ = 'X' .
          PERFORM grp_summ_list .
        ENDIF.
      ELSE .
      MESSAGE i000(8i) WITH 'No Records exist as per Selection Condititon' .
      ENDIF .
    TOP-OF-PAGE .
      WRITE : / text-011 , ' - ' , sy-datum .
    *&      Form  DISP_LIST
          text
    -->  p1        text
    <--  p2        text
    FORM disp_list.
      LOOP AT itab.
    ********changes made by dhiraj - 23.11.2007********************
        AT NEW bzirk.
          SKIP .
         SELECT SINGLE * FROM t171t  WHERE bzirk = itab-bzirk
                                     AND   spras = 'E'   .
          SELECT SINGLE * FROM t005u WHERE spras = sy-langu
                                              AND BLAND = itab-bzirk AND LAND1 = ITAB-LAND1.
          PERFORM list_heading.
          FORMAT COLOR 4 INTENSIFIED ON.
          WRITE :/ 'Sales Region : ',itab-bzirk,30 t005u-BEZEI
                                        ,81 ''.
          ULINE .
        ENDAT.
        AT NEW kunde.
          SELECT SINGLE * FROM kna1 WHERE kunnr = itab-kunde .
          FORMAT COLOR 3 INTENSIFIED ON.
          WRITE :/ 'Dealer         : ',itab-kunde,30(25) kna1-name1,
                 60 'City  : ' ,kna1-ort01, 81 ''.
          ULINE .
        ENDAT.
        AT NEW fkdat .
          FORMAT COLOR OFF INTENSIFIED OFF .
          WRITE : /5 itab-vbeln,
                   20 itab-fkdat .
          CLEAR flag_fkdat .
        ENDAT .
       at new matkl .
         format color 2 intensified on.
         write :/ 'Material Group    : ',itab-matkl, 81 ''.
         skip .
       endat .
       mcnt = mcnt + 1 .
        dealer-days1 = dealer-days1 + itab-days1  .
        dealer-days2 = dealer-days2 + itab-days2  .
        dealer-days3 = dealer-days3 + itab-days3  .
        dealer-days4 = dealer-days4 + itab-days4  .
        dealer-days5 = dealer-days5 + itab-days5  .
        state-days1  = state-days1 + itab-days1  .
        state-days2  = state-days2 + itab-days2  .
        state-days3  = state-days3 + itab-days3  .
        state-days4  = state-days4 + itab-days4  .
        state-days5  = state-days5 + itab-days5  .
        IF flag_fkdat EQ 'Y'  .
          NEW-LINE .
        ENDIF .
        WRITE :    35 itab-matnr, "krunal
                   55 itab-sernr,
                   72 itab-werks, "krunal
                   83 itab-days1 ,
                   93 itab-days2 ,
                   103 itab-days3 ,
                   113 itab-days4 ,
                   123 itab-days5 .
        PERFORM get_musa_ta.
        flag_fkdat = 'Y' .
       at end of matkl .
         skip .
         format color 2 intensified on.
         write :/ 'Total For    : ',itab-matkl, ' - ' , mcnt .
         clear mcnt .
         uline  .
       endat .
        AT END OF kunde.
          dealer_total = dealer-days1 + dealer-days2 + dealer-days3
                                     + dealer-days4 + dealer-days5 .
          FORMAT COLOR 3 INTENSIFIED ON.
          WRITE :/ 'Total for  : ',itab-kunde,' - ' ,
                   79 dealer-days1 ,
                   89 dealer-days2 ,
                   99 dealer-days3 ,
                   109 dealer-days4 ,
                   119 dealer-days5 ,
                   129 dealer_total  .
          CLEAR : dealer_total , dealer .
          ULINE .
        ENDAT.
        AT END OF bzirk.
          state_total = state-days1 + state-days2 + state-days3
                                    + state-days4 + state-days5 .
          FORMAT COLOR 4 INTENSIFIED ON.
          WRITE :/ 'Total for  : ',itab-bzirk,' - ' ,
                   79 state-days1 ,
                   89 state-days2 ,
                   99 state-days3 ,
                   109 state-days4 ,
                   119 state-days5 ,
                   129 state_total  .
          CLEAR : state_total , state .
          ULINE.
        ENDAT.
      ENDLOOP.
    ENDFORM.                               " DISP_LIST
    *&      Form  LIST_HEADING
          text
    -->  p1        text
    <--  p2        text
    FORM list_heading.
      DATA : text1(7) , text2(7) , text3(7) , text4(7) , text5(7),
             text11(4) , text12(4) , text13(4) , text14(4) , text15(4) .
      text11 = days1 .
      text12 = days2 .
      text13 = days3 .
      text14 = days4 .
      text15 = days5 .
      CONCATENATE : '=<' text11 INTO text1 ,
                    '>'  text12 INTO text2 ,
                    '>'  text13 INTO text3 ,
                    '>'  text14 INTO text4 ,
                    '>'  text15 INTO text5 .
      ULINE.
      FORMAT COLOR 1 INTENSIFIED OFF.
      WRITE :/5  'Invoice No.',
              20 'Invoice Dt',
              34 'Material',
              55 'Serial No .' ,
              72 'Plant',
              79  text1 ,
              89  text2 ,
              99  text3 ,
              109  text4 ,
              119  text5 ,
              129 'Floor' ,
              139  'Total'  .
      ULINE.
    ENDFORM.                               " LIST_HEADING
    *&      Form  SUMMARY_LIST
          text
    -->  p1        text
    <--  p2        text
    FORM summary_list.
      CLEAR : us, us_total .
      LOOP AT itab.
        AT FIRST.
          SKIP .
    ******modified by dhiraj- 23.11.2007*******************************
         SELECT SINGLE * FROM t171t  WHERE bzirk = itab-bzirk
                                     AND   spras = 'E'   .
          SELECT SINGLE * FROM t005u WHERE spras = sy-langu
                                              AND BLAND = itab-bzirk AND LAND1 = ITAB-LAND1.
          PERFORM list_heading_summary.
         format color 4 intensified on.
         write :/ 'Sales District : ',itab-bzirk,30 t171t-bztxt
                                       ,81 ''.
          ULINE .
        ENDAT.
        AT NEW kunde.
          SELECT SINGLE * FROM kna1 WHERE kunnr = itab-kunde .
         format color 3 intensified on.
         write :/ 'Dealer         : ',itab-kunde,30(25) kna1-name1,
                60 'City  : ' ,kna1-ort01, 81 ''.
         uline .
        ENDAT.
        AT NEW fkdat .
         format color off intensified off .
         write : /5 itab-vbeln,
                  20 itab-fkdat .
          CLEAR flag_fkdat .
        ENDAT .
       at new matkl .
         format color 2 intensified on.
         write :/ 'Material Group    : ',itab-matkl, 81 ''.
         skip .
       endat .
       mcnt = mcnt + 1 .
        dealer-days1 = dealer-days1 + itab-days1  .
        dealer-days2 = dealer-days2 + itab-days2  .
        dealer-days3 = dealer-days3 + itab-days3  .
        dealer-days4 = dealer-days4 + itab-days4  .
        dealer-days5 = dealer-days5 + itab-days5  .
        state-days1  = state-days1 + itab-days1  .
        state-days2  = state-days2 + itab-days2  .
        state-days3  = state-days3 + itab-days3  .
        state-days4  = state-days4 + itab-days4  .
        state-days5  = state-days5 + itab-days5  .
        us-days1  = us-days1 + itab-days1  .
        us-days2  = us-days2 + itab-days2  .
        us-days3  = us-days3 + itab-days3  .
        us-days4  = us-days4 + itab-days4  .
        us-days5  = us-days5 + itab-days5  .
        IF flag_fkdat EQ 'Y'  .
          NEW-LINE .
        ENDIF .
       write :    42 itab-sernr ,
                  59 itab-days1 ,
                  69 itab-days2 ,
                  79 itab-days3 ,
                  89 itab-days4 ,
                  99 itab-days5 .
        flag_fkdat = 'Y' .
       at end of matkl .
         skip .
         format color 2 intensified on.
         write :/ 'Total For    : ',itab-matkl, ' - ' , mcnt .
         clear mcnt .
         uline  .
       endat .
        AT END OF kunde.
          dealer_total = dealer-days1 + dealer-days2 + dealer-days3
                                     + dealer-days4 + dealer-days5 .
         format color 3 intensified on.
         write :/ 'Total for  : ',itab-kunde,' - ' ,
                  57 dealer-days1 ,
                  67 dealer-days2 ,
                  77 dealer-days3 ,
                  87 dealer-days4 ,
                  97 dealer-days5 ,
                  107 dealer_total  .
          CLEAR : dealer_total , dealer .
         uline .
        ENDAT.
        AT END OF bzirk.
    **************modified by dhiraj - 23.11.2007****************
         SELECT SINGLE * FROM t171t  WHERE bzirk = itab-bzirk
                                     AND   spras = 'E'   .
          SELECT SINGLE * FROM t005u WHERE spras = sy-langu
                                              AND BLAND = itab-bzirk AND LAND1 = ITAB-LAND1.
          state_total = state-days1 + state-days2 + state-days3
                                    + state-days4 + state-days5 .
          FORMAT COLOR 4 INTENSIFIED ON.
          WRITE :/10 t171t-bztxt,   " 'Total for  : ',itab-bzirk,' - ' ,
                   57 state-days1 ,
                   67 state-days2 ,
                   77 state-days3 ,
                   87 state-days4 ,
                   97 state-days5 ,
                   107 state_total  .
          CLEAR : state_total , state .
          ULINE.
        ENDAT.
        us_total = us-days1 + us-days2 + us-days3 + us-days4 + us-days5 .
      ENDLOOP.
      SKIP 2.
      FORMAT COLOR 5 INTENSIFIED ON.
      WRITE :/10 'Total for US : ' ,
                55 us-days1 ,
                65 us-days2 ,
                75 us-days3 ,
                85 us-days4 ,
                95 us-days5 ,
                105 us_total  .
    ENDFORM.                               " SUMMARY_LIST
    *&      Form  LIST_HEADING_SUMMARY
          text
    -->  p1        text
    <--  p2        text
    FORM list_heading_summary.
      DATA : text1(7) , text2(7) , text3(7) , text4(7) , text5(7),
             text11(4) , text12(4) , text13(4) , text14(4) , text15(4) .
      text11 = days1 .
      text12 = days2 .
      text13 = days3 .
      text14 = days4 .
      text15 = days5 .
      CONCATENATE : '=<' text11 INTO text1 ,
                    '>'  text12 INTO text2 ,
                    '>'  text13 INTO text3 ,
                    '>'  text14 INTO text4 ,
                    '>'  text15 INTO text5 .
      ULINE.
      FORMAT COLOR 1 INTENSIFIED OFF.
      WRITE :/10  'State',
              57  text1 ,
              67  text2 ,
              77  text3 ,
              87  text4 ,
              97  text5 ,
              110  'Total'  .
      ULINE.
    ENDFORM.                               " LIST_HEADING_SUMMARY
    *&      Form  LIST_HEADING_SUMMARY
          text
    -->  p1        text
    <--  p2        text
    FORM list_heading_grp_summary.
      DATA : text1(7) , text2(7) , text3(7) , text4(7) , text5(7),
             text11(4) , text12(4) , text13(4) , text14(4) , text15(4) .
      text11 = days1 .
      text12 = days2 .
      text13 = days3 .
      text14 = days4 .
      text15 = days5 .
      CONCATENATE : '=<' text11 INTO text1 ,
                    '>'  text12 INTO text2 ,
                    '>'  text13 INTO text3 ,
                    '>'  text14 INTO text4 ,
                    '>'  text15 INTO text5 .
      ULINE.
      FORMAT COLOR 1 INTENSIFIED OFF.
      WRITE :/10  'Material Group',
              57  text1 ,
              67  text2 ,
              77  text3 ,
              87  text4 ,
              97  text5 ,
              110  'Total'  .
      ULINE.
    ENDFORM.                               " LIST_HEADING_GRP_SUMMARY
    *&      Form  GET_MUSA_TA
          text
    -->  p1        text
    <--  p2        text
    FORM get_musa_ta.
      CLEAR vbpa .
      SELECT SINGLE    * FROM  vbpa
             WHERE  vbeln  = itab-vbeln
             AND    parvw  =  'RE' .
          and    kunnr  = 'TA001' .
      IF sy-subrc = 0 .
        IF vbpa-kunnr = 'TA001' .
          WRITE : 129 'TA ' .
        ELSE.
          WRITE : 129 'MUSA' .
        ENDIF.
      ENDIF.
    ENDFORM.                               " GET_MUSA_TA
    *&      Form  grp_summ_list
          text
    -->  p1        text
    <--  p2        text
    FORM grp_summ_list .
      CLEAR : us, us_total .
    itab_grp[] = itab[] .
    sort itab_grp BY werks matkl.
      LOOP AT itab_grp.
        AT FIRST.
          SKIP .
          PERFORM list_heading_grp_summary.
          ULINE .
        ENDAT.
        matgr-days1  = matgr-days1 + itab_grp-days1  .
        matgr-days2  = matgr-days2 + itab_grp-days2  .
        matgr-days3  = matgr-days3 + itab_grp-days3  .
        matgr-days4  = matgr-days4 + itab_grp-days4  .
        matgr-days5  = matgr-days5 + itab_grp-days5  .
        plant-days1 = plant-days1 + matgr-days1 .
        plant-days2 = plant-days2 + matgr-days2 .
        plant-days3 = plant-days3 + matgr-days3 .
        plant-days4 = plant-days4 + matgr-days4 .
        plant-days5 = plant-days5 + matgr-days5 .
        us-days1  = us-days1 + itab_grp-days1  .
        us-days2  = us-days2 + itab_grp-days2  .
        us-days3  = us-days3 + itab_grp-days3  .
        us-days4  = us-days4 + itab_grp-days4  .
        us-days5  = us-days5 + itab_grp-days5  .
        IF flag_fkdat EQ 'Y'  .
          NEW-LINE .
        ENDIF .
       write :    42 itab-sernr ,
                  59 itab-days1 ,
                  69 itab-days2 ,
                  79 itab-days3 ,
                  89 itab-days4 ,
                  99 itab-days5 .
        flag_fkdat = 'Y' .
        AT END OF matkl.
          matgr_total = matgr-days1 + matgr-days2 + matgr-days3
                                    + matgr-days4 + matgr-days5 .
          FORMAT COLOR 4 INTENSIFIED ON.
          WRITE :/10 itab_grp-matkl,   " 'Total for  : ',itab_grp-matkl,' - ' ,
                   57 matgr-days1 ,
                   67 matgr-days2 ,
                   77 matgr-days3 ,
                   87 matgr-days4 ,
                   97 matgr-days5 ,
                   107 matgr_total  .
          CLEAR : matgr_total , matgr .
          ULINE.
        ENDAT.
        AT END OF werks.
          plant_total = plant-days1 + plant-days2 + plant-days3
                                    + plant-days4 + plant-days5 .
          FORMAT COLOR 4 INTENSIFIED ON.
          WRITE :/10 itab_grp-werks,   " 'Total for  : ',itab_grp-werks,' - ' ,
                   57 plant-days1 ,
                   67 plant-days2 ,
                   77 plant-days3 ,
                   87 plant-days4 ,
                   97 plant-days5 ,
                   107 plant_total  .
          CLEAR : plant_total , plant .
          ULINE.
        ENDAT.
        us_total = us-days1 + us-days2 + us-days3 + us-days4 + us-days5 .
      ENDLOOP.
      SKIP 2.
      FORMAT COLOR 5 INTENSIFIED ON.
      WRITE :/10 'Total  : ' ,
                55 us-days1 ,
                65 us-days2 ,
                75 us-days3 ,
                85 us-days4 ,
                95 us-days5 ,
                105 us_total  .
    ENDFORM.                    " grp_summ_list

    {SELECT OBJEK CUOBJ
      INTO CORRESPONDING FIELDS OF TABLE TAB_INOB
      FROM INOB
    WHERE OBJEK IN R_MATNR.
    SELECT ATWRT OBJEK ATINN
      INTO CORRESPONDING FIELDS OF TABLE TAB_AUSP
      FROM AUSP
       FOR ALL ENTRIES IN TAB_INOB
    WHERE OBJEK = TAB_INOB-CUOBJ.
    SELECT ATNAM
      INTO TABLE TAB_CABN
      FROM CABN
       FOR ALL ENTRIES IN TAB_INOB
    WHERE ATNAM = 'ZCURSEASON'
        OR ATNAM = 'ZCURYEAR'
       AND ATINN = TAB_AUSP-ATINN.
    after this you can gether the data into one internal table.
    Form one inter nal table it_final.
    loop at TAB_INOB.
    it_final-OBJEK = tab_inob-OBJEK.
    it_final-CUOBJ = tab_inob-CUOBJ.
    READ TABLE TAB_CABN  WITH KEY OBJEK = TAB_INOB-CUOBJ.
    IT_FINAL-ATWRT = TAB_CABN-ATWRT.
    IT_FINAL-ATINN = TAB_CABN-ATINN.
    DO SAME FOR THIRD TABLE
    AFFTER DO ALL
    APPEND IT_FINAL.
    ENDLOOP.}

  • Inner Joins vs For All Entries - performance query

    Hi All,
    I'm a bit confused here...  I see lots and lots (and lots...) of postings from people asking how to get data from multiple tables.
    To me the immediate answer is to use joins in my select statement to reduce the database load but more and more I see people suggesting FOR ALL ENTRIES is better from a performance perspective.
    Now, simple question time, which is more efficient in the real world when doing something like the following:- (this is a basic example but I'm sure you know what I mean.)
    Select  *
      into  table lt_sales_data
      from  vbap as vbap
    inner  join vbak as vbak
         on vbak~vbeln eq vbap~vbeln
      where  vbak~vbeln in so_vbeln.
    or
    Select  *
      into  table lt_vbak_data
      from  vbak
    where  vbeln in so_vbeln.
    if lt_vbak_data[] is not initial.
    select  *
      into  table lt_vbap_data
      from  vbap
      for all entries in lt_vbak_data
    where  vbeln eq lt_vbak_data-vbeln.
    endif.
    Basically I want to know whether joins or for all entries is better from a database performance perspective.
    I want to know why as well so please don't just post links, random cut and paste answers or one liners.  I'm convinced for all entries is slower but am willing to be persuaded otherwise if someone can show me proof.
    Thanks,
    Gareth.

    Thanks to all the opinions so far...  You've backed up what I suspected although I maybe wasn't clear enough with my question and desired result.  I was hoping someone could produce some hard and fast guidelines from SAP themselves dictating which is the better method.  I know 10 years ago I was taught to use joins and to keep my database access to minimal levels, using keyed reads where applicable and using internal tables to filter data where I couldn't use key fields.
    Gautham, I am aware of SM30 but that doesn't answer the general question I was asking.  I've obviously used SM30 to run some comparisons but I was really hoping someone could give me a definitive answer based on hard facts or from attending some training at SAP recently.  And no points for asking for them
    Tamás, I agree with what you are saying about runing many comparisons - it appears to be dependant on any number of criteria which means each case may require different code.  I've not managed to find a consistent comparison of the two methods that would lead me to use one method or the other...
    Karan, thanks for your feelings but it doesn't really help me!  Why/how does it retrieve the data faster than a join?  Have you got testing/proof to back this up?
    Raam, thanks for those links - they are interesting reads and seem to go through the same arguments I'm currently considering.  Although I still don't have a definitie answer!
    Amit, I understand exactly what yuo are saying about myths and urban legends   That was my motivation for this post.  To bo honest, it seems to me that a lot of the "newer" ABAP coders always go for FOR ALL ENTRIES but I wanted to know - is there a reason or do they all just cut and paste off SDN?!  Are they all just scared of complex inner joins?!  What would you all make of this Select statement for example? -   
    select  afvc~arbid
                afko~aufnr
                aufk~objnr
                afko~plnnr
                afko~plnal
                afko~aufpl
                afko~zaehl
                afpo~matnr
                makt~maktx
                afvc~vornr
                afvc~ltxa1
                afvu~aplzl
                afvu~usr10
                afvv~meinh
                afvv~bmsch
                afvv~vge02
                afvv~vgw02
                afvv~mgvrg
                afab~aplzl_vor
          into  table lt_recipe_orders
          from  afko as afko
         inner  join aufk as aufk
            on  aufk~aufnr eq afko~aufnr
         inner  join afpo as afpo
            on  afpo~aufnr eq afko~aufnr
         inner  join makt as makt
            on  makt~matnr eq afpo~matnr
         inner  join afvc as afvc
            on  afvc~aufpl eq afko~aufpl
         inner  join afvu as afvu
            on  afvu~aufpl eq afvc~aufpl
           and  afvu~aplzl eq afvc~aplzl
         inner  join afvv as afvv
            on  afvv~aufpl eq afvu~aufpl
           and  afvv~aplzl eq afvu~aplzl
          left  outer join afab as afab
            on  afab~aufpl_nch eq afvu~aufpl
           and  afab~aplzl_nch eq afvu~aplzl
           for  all entries in t_resources
         where  afko~gltrs ge v_start_date
           and  afko~gstrs le v_start_date
           and  afko~plnty eq gc_task_list_type_2
           and  afpo~dwerk eq v_werks
           and  makt~spras eq sy-langu
           and  afvc~arbid eq t_resources-objid.
    Twinkal, I've always thought less DB hits is a better performing program too - the above example compares 2 db hits to 1...  I don't have issues with complex joins because I've used them so long so can discount that problem but do agree that less DB hits is the ultimate goal.  Providing of course the Selects you write are actually efficient in themselves.
    Murthy, if you build your select statement correctly duplicate records can be avoided in most cases.  How can you say a join statement will hit the database more when in my example there is 1 DB hit compared to 2 for a for all entries?  And I'd love to know the reasoning behind never using a join on more than 2 tables?!  Is that just an urban myth?!
    Thomas, I've just been looking at some of Siegfried's posts and like what I am reading.  As you say, using full keys via joins is essential.
    Gareth.

  • Can DRIVER itab & RESULTANT  itab  be  same with  FOR ALL  ENTRIES ??

    Hi All,
         Can DRIVER itab & RESULTANT  itab  be  same with  FOR ALL  ENTRIES ??
         Whole idea is  to  update one field  of  ITAB  from another table ....
    Regards
    Jaman
    Edited by: ABAP Techie on Sep 11, 2008 8:25 AM

    I found this in the F1-Help for "FOR ALL ENTRIES":
    >"In Release 6.10 and higher, the same internal table can be specified after FOR ALL ENTRIES and after INTO."
    Check however if you can use a proper JOIN select. This will fill your internal table in one operation and is usually faster than a FOR ALL ENTRIES, contrary to some circulating comments here.
    Thomas

  • For All Entries is NOT better than INNER JOIN in most cases

    I quote from Siegfried Boes' excellent post here: Will writing an inner join be better or creating a view?
    For all the FOR ALL ENTRIES lovers ... there is no proof for these reappearing recommendation.
    There is nearly nobody who receives forum points, who recommends FOR ALL ENTRIES instead of Joins. What is the reason ???
    It is easier to prove the opposite. A Join is a nested loop inside the database, a FOR ALL ENTRIES is partly outside of the database. FOR ALL ENTRIES works in blocks, joins on totals.
    FOR ALL ENTRIES are not recommded on really large tables, because the chances are too high that
    too many records are transferred.
    People prefer FOR ALL ENTRIES, because JOINs are not so easy to understand. Joins can go wrong, but with a bit of understanding they can be fixed.
    Some Joins are slow and can not be fixed, but then the FOR ALL ENTRIES would be extremely slow.
    There are several kinds of views:
    - projection views, i.e. only one table involved just fields reduced
    - join views, several tables, joins conditions stored in dictionary
    - materialized views, here the joined data are actually stored in the database. Storing and synchronisation has to be done manually.
    Only the last one creates real overhead. It should be the exception.
    Join Views and Joins are nearly identical. The view is better for reuse. The join is better in complicated, becuase if the access goes wrong, it can often be fixed by adding a hint. Hints can not be added to views.
    Abraham Bukit  points out:
    If it is cluster table, (you can't use join). If it is buffered table, I would also say avoid join.
    If they all are transaction table which are not buffered and are not cluster tables.  
    He further supports Siegfried's statement that FAE is easier to undestand than INNER JOINs.
    Thomas Zloch says, regarding buffered tables:
    At least think twice, maybe compare runtimes if in doubt. 
    So, unless someone has some EVIDENCE that FOR ALL ENTRIES is better, I don't think we want to see this discussed further.
    Kind regards
    Matt

    To give food for thought here's an example I  gave in a thread:
    If you have a statement like
    SELECT ... FOR ALL ENTRIES IN FAE_itab WHERE f = FAE_itab-f.
    SAP sends it to the database depending how the parameter rsdb/prefer_union_all is set:
    rsdb/prefer_union_all = 0 =>
    SELECT ... WHERE f = FAE_itab[1]-f
              OR    f = FAE_itab[2]-f
              OR    f = FAE_itab[N]-f
    You have some influence  of the generated statement type: Instead of OR'ed fields an IN list can be used
    if you have only a single coulmn N to compare:
    rsdb/prefer_in_itab_opt parameter:
    SELECT ... WHERE f IN (itab[1]-f, itab[2]-f, ..., itab[N]-f)
    rsdb/prefer_union_all = 1 =>
    SELECT ... WHERE f = FAE_itab[1]-f
    UNION ALL SELECT ... WHERE f = FAE_itab[2]-f
    UNION ALL SELECT ... WHERE f = FAE_itab[N]-f
    see: Note 48230 - Parameters for the SELECT ... FOR ALL ENTRIES statement
    As you can see for the 2nd parameter several statements are generated and combined with a UNION ALL,
    the first setting generates statements with OR's (or uses IN  if possible) for the entries in FAE_itab.
    I give you a little example here (my parameters are set in a way that the OR's are translated to IN lists; i traced the execution in ST05)
    Select myid into table t_tabcount from mydbtable
      for all entries in t_table    " 484 entries
        where myid = t_table-myid .
    ST05 trace:
    |Transaction SEU_INT|Work process no 0|Proc.type  DIA|Client  200|User |
    |Duration |Obj. name |Op.    |Recs.|RC    |Statement|
    | 640|mydbtable |PREPARE|   |  0|SELECT WHERE "myid" IN ( :A0 , :A1 , :A2 , :A3 , :A4 ) AND "myid" = :A5|
    | 2|mydbtable |OPEN   |   |  0|SELECT WHERE "myid" IN ( 1 , 2 , 3 , 4 , 5 ) AND "myid" = 72 |
    | 2.536|mydbtable |FETCH  |    0|  1403|   |
    | 3|mydbtable |REOPEN |   |  0|SELECT WHERE "myid" IN ( 6 , 7 , 8 , 9 , 10 ) AND "myid" = 72 |
    | 118|mydbtable |FETCH  |  0|  |
    | 2|mydbtable |REOPEN |  |  0|SELECT WHERE "myid" IN ( 11 , 12 , 13 , 14 , 15 ) AND "myid" = 72     |
    | 3|mydbtable |REOPEN |  |  0|SELECT WHERE "myid" IN ( 475 , 476 , 477 , 478 , 479 ) AND "myid" = 72  |
    | 94|mydbtable |FETCH  | 0| 1403|   |
    | 2|mydbtable |REOPEN |   |  0|SELECT WHERE "myid" IN ( 480 , 481 , 482 , 483 , 484 ) AND "myid" = 72 |
    You see the IN list contained 5 entries each , wich made up about 97 statements for all 484 entries.
    For every statment you have a single fetch operation wich means a separate access to the database.
    If you would replace the FAE with a join you would only have one fetch to the database.
    With the example above we can derive these observations:
    1. From database point of view these settings kill performance when you access a big table and/or have a lot of entries or columns in your FAE_itab. Furthermore, you hide information what data you will access
    at all and thus you block the database from creating a more efficient execution plan because it DOESN'T KNOW wich data you will select in the next step. I.e. it may be more efficient to scan the table in one shot instead of having many index accesses - but the database can make this decision only if it can examine ONE statement that has ALL the information of what data to retrieve.
    2. A second impact is that with every statement execution you trigger the allocation of database resources
    wich will contribute to the overhead described above.
    Said that, FAE  can never be a replacement for joining big tables (think of having a table with thousands of records in a FAE table )
    Edited by: kishan P on Nov 2, 2010 2:16 PM - Format Fixed

  • FOR ALL ENTRIES IN A JOIN

    Hi Guyz,
    My requirement is to get data from 2 tables ..i wrote  select statement  but i wanna know if there is any otherway to do it for better peformance..
    SELECT vbfa~vbeln
               vbfa~vbelv
               vbfa~rfmng
               vbfa~vbtyp_n
               ltap~pquit
               INTO TABLE gt_del
               FROM vbfa
               LEFT OUTER JOIN
               ltap  ON
               vbfavbeln = ltaptanum
               AND vbfalgnum = ltaplgnum
               FOR ALL entries IN gt_ltap
               WHERE  vbelv = gt_data
               AND vbfa~vbeln = gt_ltap-tanum
               AND vbfa~lgnum = gt_ltap-lgnum
               AND vbfa~vbtyp_n = 'Q'.
    gt_data  having delvery numbers from the file.
    plz advise..
    regds

    Hi
    Use FOR ALL ENTRIES
      select lgnum       " Warehouse Number / Warehouse Complex
             lgtyp       " Storage Type
             lgpla       " Storage Bin
             skzua       " Blocking Indicator: For Stock Removals (User)
             skzue       " Blocking indicator: for putaways (user)
             spgru       " Blocking reason
             anzle       " Number of storage units in storage bin
             maxle       " Max number of storage units in storage bin
             btanr       " Transfer order number of last stock transfer
             btaps       " Item in transfer order of last stock transfer
             kzler       " Indicator whether storage bin is empty
             kzvol       " Indicator whether storage bin is full
      from lagp
      into table t_lagp
      where lgnum in s_lgnum.
      if t_lagp[] is not initial.
        select lgnum       " Warehouse Number / Warehouse Complex
               tanum       " Transfer Order Number
               mblnr       " Number of Material Document
          from ltak
          into table t_ltak
          for all entries in t_lagp
          where lgnum eq t_lagp-lgnum.
      endif.
      if t_ltak[] is not initial.
        select lgnum       " Warehouse Number / Warehouse Complex
               tanum       " Transfer Order Number
               tapos       " Transfer order item
               posnr       " Item number of the SD document
               matnr       " Material Number
               werks       " Plant
               charg       " Batch Number
               bestq       " Stock Category in the Warehouse Management
    *System
               maktx       " Material Description
        from ltap
        into table t_ltak
        for all entries in t_ltak
        where lgnum eq t_ltak-lgnum and tanum eq t_ltak-tanum.
      endif.
    Like this we have to filter the entires.
    Thanks & Regards,
    Chandralekha.

Maybe you are looking for

  • How to find out the user from the Jobs queue in Report server

    Hello All! I have a doubt about finding out the user from the scheduled jobs queue. Say I go ahead and schedule a report on Reports Server how can I find out the user name. When I view the jobs using showjobs I could see that the DBMS_JOBS table has

  • Exit Buttons in ESS - SKL component.

    Hi all,     I am having an issue with the exit buttons in our ESS/SKL or the qualifications component.  The Exit buttons on these screens seem to be visible to some and not to others.  I initally thought that it might be a role issue, or maybe a code

  • About menu

    hai everybody iam getting problem in open_form property in menu calling i used a button when it pressed it has to perform follwing task DELETE FROM PAPER_PLAN WHERE CARDNO = :JOBCARD1.CARDNO; DELETE FROM PLATE_FILM WHERE CARDNO = :JOBCARD1.CARDNO; DE

  • Hotkey package missing from Update Retriever

    Hi, I am unable to download (find) Hotkey package in Update Retriever, for deployment of x240 and t440 computers. Is that by design or something that can be fixed?

  • Printing from a Samsung Galaxy S5 to a HP Photosmart 3110 all-in-one

    I'm trying to print off some pictures from my Samsung Galaxy S5 to my Photosmart 3110 all-in-one printer. Tried to use two different usb cables to try and print them off as I don't have a working computer and there's no WiFi thing connected to the pr