SELECT FOR ALL ENTRIES vs LOOP SELECT SINGLE in ABAP

Ailin Albertoni picture Ailin Albertoni · Oct 9, 2013 · Viewed 26.9k times · Source

I am comparing the SELECT FOR ALL ENTRIES vs LOOP SELECT SINGLE and I am having a big difference between results.

Somebody can help me with this? What is the difference?

SELECT ekko~ebeln
    ekpo~ebelp
    ekko~bstyp
    ekko~bsart
    ekko~lifnr
    ekko~ekgrp
    ekko~aedat
    ekko~knumv
    ekko~zterm
    ekko~zbd1t
    ekpo~matnr
    ekpo~txz01
    ekpo~menge
    ekpo~meins
    ekpo~netpr
    ekpo~peinh
    ekpo~knttp
    ekpo~afnam
    ekpo~wepos
INTO TABLE t_ekpo
FROM ekko
INNER JOIN ekpo ON
    ekpo~ebeln EQ ekko~ebeln
WHERE ekko~lifnr IN s_lifnr
    AND ekko~ekgrp IN s_ekgrp
    AND ekko~ebeln IN s_ebeln
    AND ekko~aedat IN s_aedat
    AND ekko~bsart IN s_bsart
    AND ekko~bstyp EQ 'F'
    AND ekko~loekz EQ space
    AND ekpo~matnr IN s_matnr
    AND ekpo~ebelp IN s_ebelp
    AND ekpo~loekz EQ space.

SELECT FOR ALL ENTRIES returns 9.528 records

SELECT ebeln
    ebelp
    zekkn
    vgabe
    gjahr
    belnr
    buzei
    budat
    menge
    shkzg
FROM ekbe
INTO TABLE t_ekbe
FOR ALL ENTRIES IN t_ekpo
WHERE ebeln EQ t_ekpo-ebeln
    AND ebelp EQ t_ekpo-ebelp
    AND vgabe IN ('1','2').

LOOP SELECT SINGLE returns 7.336 records

LOOP AT t_ekpo INTO gs_ekpo.

    CLEAR gs_ekbe.

    SELECT SINGLE  ebeln
                 ebelp
                 zekkn
                 vgabe
                 gjahr
                 belnr
                 buzei
                 budat
                 menge
                 shkzg
    FROM ekbe
    INTO gs_ekbe
    WHERE ebeln EQ gs_ekpo-ebeln
        AND ebelp EQ gs_ekpo-ebelp
        AND vgabe IN ('1','2').

    APPEND gs_ekbe TO T_ekbe.

ENDLOOP.

Answer

Gert Beukema picture Gert Beukema · Oct 9, 2013

Table EKBE has more key fields than just EBELN and EBELP, so there can be more entries for a unique EBELN/EBELP pair. With the SELECT SINGLE you will not get these extra entries, with the FOR ALL ENTRIES you do. Depending on your needs only one of these will get you the correct data set.