Comments

ABAP FOR ALL ENTRIES SELECT statement addition in SAP data retrieval




The ABAP select statement command has a number of options and additions which allows you to fine tune your database selection. One such addition is the 'FOR ALL ENTRIES' statement. This allows you to build an internal table and then restrict thew second database select to only get entries contained in this itab.

Below is a simple ABAP code example of how to select data using the 'FOR ALL ENTRIES' addition! It basically builds an internal table containing a list of purchase order numbers(EBELN) from table EKKO and then only retrieves entries from EKPO where the PO number exists in this internal table.

There is a strange feature that happens if you use an empty internal table as the for entries restriction along side a basic where clause.. see below for full details and example ABAP code.

REPORT  ZSELECTCOMMAND.
*Select FOR ALL ENTRIES command
data: it_ekko type STANDARD TABLE OF ekko,
      wa_ekko like line of it_ekko,
      it_ekpo type standard table of ekpo,
      wa_ekpo like line of it_ekpo.

SELECT *
  UP TO 10 ROWS  "only return first 10 hits
  FROM ekko
  INTO TABLE it_ekko.
loop at it_ekko into wa_ekko.
  write:/ wa_ekko-ebeln.
endloop.

IF sy-subrc EQ 0.
* The FOR ALL ENTRIES comand only retrieves data which matches
* entries within a particular internal table.
  SELECT *
    FROM ekpo
    INTO TABLE it_ekpo
    FOR ALL ENTRIES IN it_ekko
    WHERE ebeln EQ it_ekko-ebeln.
  write:/ 'FOR ALL ENTRIES comand '.
  loop at it_ekpo into wa_ekpo.
    write:/ wa_ekpo-ebeln, wa_ekpo-ebelp.
  endloop.
ENDIF.

Empty FOR ALL ENTRIES itab ignores standard where clause

This leads me onto the strange feature mentioned above that I would not really expect to happen, although I'm sure there is a logical reason for it. If you use the FOR ALL ENTRIES addition, but use an itab that is empty and you also use a standard where clause entry such as where "EBELN EQ WA_EKKO-EBELN" what would you expect to happen?

....Remember an empty for all entries table returns everything!!! but would you expect it to still restrict on the other where clause entry? See code below for demonstration of what happens (SPOILER ALERT: EBELN EQ WA_EKKO-EBELN is ignored and all entries are retrieved).

*Empty for all entries table
DATA: gd_lines TYPE i.

SELECT *
   FROM ekpo
   INTO TABLE it_ekpo
   WHERE ebeln EQ wa_ekko-ebeln.

DESCRIBE TABLE  it_ekpo LINES gd_lines.
WRITE:/ 'Number of lines without for all entries:', gd_lines.


*Restricts based on entries in it_ekko and ebeln EQ wa_ekko-ebeln
SELECT *
  FROM ekpo
  INTO TABLE it_ekpo
  FOR ALL ENTRIES IN it_ekko
  WHERE ebeln EQ it_ekko-ebeln
    AND ebeln EQ wa_ekko-ebeln.

DESCRIBE TABLE  it_ekpo LINES gd_lines.
WRITE:/ 'Number of lines with for all entries itab:', gd_lines.


*Only difference to the above code is that it_ekko is empty
REFRESH it_ekko.

SELECT *
 FROM ekpo
 INTO TABLE it_ekpo
 FOR ALL ENTRIES IN it_ekko
 WHERE ebeln EQ it_ekko-ebeln
   AND ebeln EQ wa_ekko-ebeln.
DESCRIBE TABLE  it_ekpo LINES gd_lines.
WRITE:/ 'Number of lines with empty for all entries itab:', gd_lines.

Result output





sapdev logo background
sapdev logo sapdev logo


Can't find something on ERPWorkbench? A quick search should fix that: