Code examples · Modern ABAP · Intermediate
ABAP SELECT: FOR ALL ENTRIES versus INNER JOIN (and its three traps)
The problem
A report must list the flight connections departing from Frankfurt together with the airline name.
How we solve it
- 1FOR ALL ENTRIEStwo selects
- 2INNER JOINone select
- 3Trapswhat goes wrong
The result
Both versions return the same list.
1. The requirement
A report must list the flight connections departing from Frankfurt together with the airline name. The data sits in two tables: SPFLI (connections) and SCARR (airlines).
2. How we design it
Two roads lead to the same list. The first reads connections, then reads airlines for the carrier ids found (FOR ALL ENTRIES). The second lets the database combine both tables in one statement (INNER JOIN). We write both, then compare their behavior and risks so the right choice is clear.
Why we decided it this way
- 1Show FOR ALL ENTRIES with its guardOld code is full of it, and you must be able to read and fix it.
- 2Prefer the join for new codeOne statement, no extra internal tables, no hidden DISTINCT, no empty-table surprise.
- 3Explain the trapsThese three mistakes are the classic production bugs and interview questions.
Before you start
- A system with the flight demo data (tables SPFLI and SCARR filled). You can check in SE16N.
3. The code, step by step
Step 1 of 3: Read with FOR ALL ENTRIES
The classic two-step read, with the guard that must never be missing.
" Console class
CLASS zcl_fae_vs_join_demo DEFINITION PUBLIC FINAL CREATE PUBLIC.
PUBLIC SECTION.
INTERFACES if_oo_adt_classrun.
ENDCLASS.
CLASS zcl_fae_vs_join_demo IMPLEMENTATION.
METHOD if_oo_adt_classrun~main.
" First select: the connections
SELECT carrid, connid, cityfrom, cityto
FROM spfli
WHERE cityfrom = 'FRANKFURT'
INTO TABLE @DATA(flights).
" Second select with FOR ALL ENTRIES and the guard
IF flights IS NOT INITIAL.
SELECT carrid, carrname
FROM scarr
FOR ALL ENTRIES IN @flights
WHERE carrid = @flights-carrid
INTO TABLE @DATA(carriers).
ENDIF.
" Combine in ABAP
LOOP AT flights INTO DATA(f).
READ TABLE carriers INTO DATA(c) WITH KEY carrid = f-carrid.
IF sy-subrc <> 0.
CLEAR c.
ENDIF.
out->write( |{ c-carrname } { f-connid } { f-cityfrom } -> { f-cityto }| ).
ENDLOOP.
Part by part (plain text)
- Console class. Run with F9 in ADT or call from a report.
- First select: the connections. Read all connections that start in Frankfurt into an internal table.
- Second select with FOR ALL ENTRIES and the guard. FOR ALL ENTRIES reads the airlines whose carrier id appears in the table flights. The IF is the guard: if the driver table is empty, FOR ALL ENTRIES ignores the WHERE condition and reads the entire table. Always check first.
- Combine in ABAP. We must merge the two tables ourselves. A READ TABLE per flight finds the name. SORT first and use BINARY SEARCH, or define carriers as a sorted table, for larger volumes.
Open in the portal
The SAP objects named in this code are linked in the code above and listed here. Each opens its page in the SAP Library.
If it does not work
4. The final outcome
Both versions return the same list. The join version is shorter and has no hidden side effects.
What you should see
With the standard flight demo data, both versions list connections from FRANKFURT with the airline name (for example Lufthansa). If you remove the emptiness check and no connection matches, the FOR ALL ENTRIES version would read ALL airlines, which is the bug.
Try it and check the result
- Run the class. Both loops print the same connections with airline names.
- Change 'FRANKFURT' to 'NOWHERE' and run: no output, no dump. Now delete the IF guard and run again: the FOR ALL ENTRIES read now returns every airline.
- Select only carrid from SPFLI with FOR ALL ENTRIES and count the rows, then do the same with a join: the counts differ, which shows the hidden DISTINCT.
Common mistakes
- Forgetting the emptiness check is the most common FOR ALL ENTRIES bug.
- For very large driver tables FOR ALL ENTRIES splits into many database statements and can become slow; a join or CDS view lets the database optimizer decide.
- FOR ALL ENTRIES stays useful when the driver data is not a database table of the same system, for example an internal table filled from an RFC call, a BAPI or user input.
- Do not claim speed without measuring. Use the SQL trace (ST05) or runtime analysis on your data volume.