Open SQL Part 3: FOR ALL ENTRIES — Correct Use
Preview · a free account unlocks the full lesson
Use FOR ALL ENTRIES safely: the empty-driver trap, duplicate removal, automatic duplicate elimination in the result, key fields in the field list, package splitting, and when a JOIN is better.
In this lessonShow sectionsHide sections
Visual overview
Explanation
Part 2 covered single-table SELECTs. Classic reports often need to read header data first and then related data for those same headers, and FOR ALL ENTRIES (FAE) is the classic tool for that second step.
The pattern looks like this: SELECT carrid connid fldate … FROM sflight INTO TABLE gt_flight WHERE carrid = p_carr. followed by SELECT bookid customid … FROM sbook INTO TABLE gt_book FOR ALL ENTRIES IN gt_flight WHERE carrid = gt_flight-carrid AND connid = gt_flight-connid AND fldate = gt_flight-fldate. FOR ALL ENTRIES (FAE) reads rows matching values from an internal "driver" table — here gt_flight. Internally, the database interface turns the driver's rows into IN lists or OR conditions and executes several statements, processed package-wise.
There are three rules that matter for correctness here. The first is to always check that the driver table is not empty before the FAE select, for instance with IF gt_flight IS NOT INITIAL. or CHECK gt_flight[] IS NOT INITIAL. If the driver table is EMPTY, the WHERE condition is ignored and the WHOLE table is read — which on SBOOK could mean millions of bookings coming back unfiltered.
Create a free account to keep reading
You have read the opening of this lesson. A free ERPClimb account unlocks the rest of it, every other lesson and topic, and your learning history, so you can pick up exactly where you stopped. No card needed.
- Full explanation
- Code example
- Real project scenario
- Common mistakes
- Best practices
- Interview angle
One tap with Google works too. Every topic keeps its first lesson open to everyone.
Keep exploring
Interview questions on this topic
Related SAP topics
Check before use: lesson examples are educational, not a guarantee of correctness for your SAP edition or release. Sample code has not been executed on your SAP system. Confirm API availability, syntax, authorizations and business impact in an approved development or test environment.