abap news
Home » ABAP JOIN @itab vs. FOR ALL ENTRIES: Which One to Use?

ABAP JOIN @itab vs. FOR ALL ENTRIES: Which One to Use?

ABAPBy xabap Published
A small table connected directly to a database

Use INNER JOIN @itab when your ABAP release (7.52 or higher) supports it and the database connection can receive internal tables. Otherwise use FOR ALL ENTRIES, and always check that the driver table is not empty.

Question FOR ALL ENTRIES JOIN @itab
Empty internal table WHERE is ignored: all rows of the client are read Inner join returns no rows
Duplicate result rows Removed silently Kept, unless you write DISTINCT
GROUP BY, aggregates No GROUP BY; only COUNT( * ) Available
ORDER BY Primary key only Any column
Fields of the internal table in the result Not possible Available
Release Old and new 7.52 and higher, database support needed

FOR ALL ENTRIES, done safely

TYPES: BEGIN OF ts_key,
         carrid TYPE spfli-carrid,
         connid TYPE spfli-connid,
       END OF ts_key.
DATA lt_keys TYPE SORTED TABLE OF ts_key WITH UNIQUE KEY carrid connid.

lt_keys = VALUE #( ( carrid = 'AA' connid = '0017' )
                   ( carrid = 'LH' connid = '0400' ) ).

IF lt_keys IS NOT INITIAL.
  SELECT carrid, connid, cityfrom, cityto
    FROM spfli
    FOR ALL ENTRIES IN @lt_keys
    WHERE carrid = @lt_keys-carrid
      AND connid = @lt_keys-connid
    INTO TABLE @DATA(lt_flights).
ENDIF.

Without the IF, an empty lt_keys would read all rows of SPFLI in the current client.

The same result with a join

SELECT s~carrid, s~connid, s~cityfrom, s~cityto
  FROM spfli AS s
  INNER JOIN @lt_keys AS k
    ON  s~carrid = k~carrid
    AND s~connid = k~connid
  ORDER BY s~carrid, s~connid
  INTO TABLE @DATA(lt_joined).

Give the internal table a defined primary key (a sorted or hashed key, or WITH EMPTY KEY). Standard keys are not evaluated and lead to a syntax warning.

Duplicates are the main difference in the result

FOR ALL ENTRIES removes rows that are identical in all selected columns. If you select only carrid and an amount and add the amounts up later, rows with the same amount vanish and the total is wrong. Select the complete key to prevent this.

A join keeps every match. If the internal table contains the same key twice, the row comes back twice. The unique sorted key above rules that out; otherwise use SELECT DISTINCT.

What happens on the database

To join with a database table, the kernel transports the internal table to the database (fast data access, FDA). Two limits follow from the SAP documentation:

  • Only one internal table can be passed to the database per statement.
  • The database must support it, otherwise an exception occurs.

You can test for support:

IF cl_abap_dbfeatures=>use_features(
     VALUE #( ( cl_abap_dbfeatures=>itabs_in_from_clause ) ) ).
  " JOIN @itab is possible
ENDIF.

The columns need elementary types. Components of type string are only allowed when they are declared with reference to the Dictionary type SSTRING.

Which one should you choose?

  1. Can the keys be selected in the same statement? Then join the database tables directly. This beats both options.
  2. Are the keys only in memory, and do release and database support it? JOIN @itab.
  3. Older release or other database? FOR ALL ENTRIES with an empty check and the full key.
  4. A handful of values for one field? A range table is enough.

Sources: SELECT, FROM @itab · FOR ALL ENTRIES · News for ABAP SQL in 7.52