ERPClimb logoERPClimb
Open SQL
ABAP Developmentintermediate 9 min read

Open SQL Part 3: FOR ALL ENTRIES — Correct Use

Preview · a free account unlocks the full lesson

Key takeaway

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 sections

Visual overview

Open SQL Part 3: FOR ALL ENTRIES — Correct Use — learning sheetIn one line: 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.ERPClimb.com / ABAP / LESSON 3/10 / LEARNING SHEETOpen SQL Part 3: FOR ALL ENTRIES — Correct Use1 · IN ONE LINEUse FOR ALL ENTRIES safely: the empty-driver trap, duplicate removal, automatic duplicateelimination in the result, key fields in the field list, package splitting, and when a JOINis better.2 · KEY IDEAS EXPLAINEDThe PatternSELECT carrid connid fldate … FROM sflight INTOTABLE gt_flight WHERE carrid = p_carr.Rule 1 — Check The Driver Is Not EmptyIF gt_flight IS NOT INITIAL. (or CHECK gt_flight[]IS NOT INITIAL) before the FAE select.Rule 2 — Remove Duplicates From The…SORT + DELETE ADJACENT DUPLICATES COMPARING thefields used in WHERE — or build a small unique keytable (Internal Tables Part 7). Fewer driver rows…Rule 3 — Include The Key FieldsFAE behaves like SELECT DISTINCT on the selectedcolumns: identical result rows are merged. If youselect only customid and price from SBOOK, two…3 · DO · AVOIDBest practice!Common mistakeAlways check IS NOT INITIAL before FAE.De-duplicate the driver on the WHERE fields.Select the full primary key.✕FAE without checking that the driver table isfilled.✕Driver table with duplicates.✕Missing key fields in the field list (rowsmerged).4 · IN A REAL PROJECTA nightly credit check selected open orders for customers listed in a file and then read items with FOR ALLENTRIES. One night the file was empty; the program read all 40 million order items, ran out of memory andblocked the batch chain until morning. The fix added an emptiness check before every FAE in the program. In a…Original ERPClimb learning sheet • Verify against your SAP release and edition

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.

Free account

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

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.