ABAP Keyword Documentation → ABAP − Reference → Processing External Data → ABAP Database Access → ABAP SQL → ABAP SQL - Reads → SELECT clauses
SELECT - FOR ALL ENTRIES
Other versions: 7.31 | 7.40 | 7.54
Syntax
... FOR ALL ENTRIES IN @itab WHERE ... col operator @itab-comp ...
Effect
If the addition FOR ALL ENTRIES is specified in front of the language element
WHERE of the statement SELECT of a
main query, the components comp of the internal table
itab specified as a
host variable here can be used in
relational expressions within
sql_cond on the right side of comparisons of a
relational operator in comparisons with a
column col. The specified
component comp must be compatible with the column col.
The internal table itab can have a structured or an elementary row type. For an elementary row type, the
pseudo component table_line must be specified for comp.
The entire logical expression sql_cond
is evaluated for each individual row of the internal table itab. The results
set of the SELECT statement is the union set of the results sets produced
by the individual evaluations. Rows that occur more than once are removed from the results set automatically. The full content of a row is considered here.
If the internal table itab is empty, the entire WHERE
condition is ignored. This means that none of the rows in the database table are skipped and are placed in the results set (once any duplicate rows are removed).
The logical expression sql_cond of the WHERE condition can comprise multiple
relational expressions using
AND and OR. However, if FOR ALL ENTRIES is specified, there must be at least one
comparison with a column of the internal table itab that can be specified statically or dynamically.
The following restrictions apply when using the addition FOR ALL ENTRIES with other additions:
- The addition
FOR ALL ENTRIESis only possible in front ofWHEREconditions in a standaloneSELECTstatement or in the main query afterOPEN CURSOR, if no common table expressions are defined usingWITH.
- The addition
FOR ALL ENTRIEScannot be used with the additionSINGLE.
- The addition
FOR ALL ENTRIEScannot be used in combination with SQL expressions, except for columns specified individually or an aggregate expressionCOUNT( * )specified individually.
- If the addition
FOR ALL ENTRIESis used, no LOB handles can be created as reader streams or as locators in the target area of a standaloneSELECTstatement.
- The addition
FOR ALL ENTRIEScannot be combined withUNION.
- In a
SELECTstatement withFOR ALL ENTRIES, no aggregate expressions except for COUNT( * ) can be used in theSELECTlist. In cases like these, the aggregate expression is not evaluated in the database, but is emulated on the AS ABAP.
- The addition
FOR ALL ENTRIESshould not be used with the additionGROUP BY. The additionGROUP BYhas no effect ifFOR ALL ENTRIESis used.
- In a
SELECTstatement withFOR ALL ENTRIES, the additionORDER BYcan only be used with the additionPRIMARY KEYand can only be used to access a single table or view. In this case, all columns of the primary key (except for the client column in client-specific tables) must be in theSELECTlist.
- No path expressions can be used in a
SELECTstatement withFOR ALL ENTRIES.
- If the addition
FOR ALL ENTRIESis used, no database fields of the built-in types STRING, RAWSTRING, and GEOM_EWKB plus LCHR and LRAW should occur in theSELECTlist. These data types prevent rows that occur more than once on the database system from being removed. These rows are only removed from the results set on the AS ABAP. If specified in theSELECTlist, a syntax check warning is raised that can be hidden by a pragma.
The internal table itab is evaluated once for each query. Any changes made
to the content of the internal table in a SELECT loop or WITH loop are ignored by the logical expression.
Notes
- The same internal table can be specified after
FOR ALL ENTRIESand afterINTO. The content of the table is evaluated byFOR ALL ENTRIESand then overwritten by theINTOclause.
- A comparison with a column of an internal table can also be performed using the
WHEREcondition of a subquery for the same data source.
- With respect to rows occurring more than once in the results set, the addition FOR
ALL ENTRIES has the same effect as when the addition
DISTINCT is specified in the definition of the selection set. Unlike
DISTINCT, the rows are not always deleted from the database system but instead are sometimes first deleted from the results set on AS ABAP. The duplicate rows are then removed from the database system if the SELECT statement can be passed to the database system as a single SQL statement. The additionDISTINCTis supported here. If theSELECTstatement needs to be distributed to multiple SQL statements before it is passed or if columns of the types STRING and RAWSTRING plus LCHR and LRAW are specified in theSELECTlist, the rows are aggregated on AS ABAP.
- If duplicate rows are first removed from AS ABAP, all rows specified by the
WHEREcondition (in some cases) are passed to an internal system table and then aggregated. The maximum size of this system table is restricted to that of normal internal tables. More specifically, the system table is always required if one of the additionsPACKAGE SIZEorUP TO,OFFSETis used simultaneously. These then have no effect on the number of rows passed from the database server to AS ABAP, but are only used when the rows are passed from the system table to the actual target area. If the maximum size of the internal system table is exceeded, a runtime error occurs.
- The addition
FOR ALL ENTRIESbypasses table buffering for tables with generic buffering if the condition afterFOR ALL ENTRIESprevents a single generic area from being specified exactly.
FOR ALL ENTRIES can be a more efficient alternative to
join expressions.
- Before using an internal table
itabafter FOR ALL ENTRIES, always check that the internal table is not initial. In an initial internal tables, all rows are read from the database regardless of any further conditions specified afterWHERE. This is not usually the required behavior.
- If the full
WHEREcondition is ignored because the internal table itab is empty, the implicit WHERE condition for the current client or the client specified usingUSING CLIENTis not affected (if implicit client handling is enabled). This means that all data is only read from the current client. If implicit client handling is disabled using the obsolete addition CLIENT SPECIFIED, no implicitWHEREcondition exists for the client. AnyWHEREcondition specified explicitly for the client column is ignored with the full condition if the internal tableitabis empty and the data from all clients is read.
- If, in a strict mode of the syntax check,
FOR ALL ENTRIESis specified together with columns of the types STRING and RAWSTRING plus LCHR and LRAW in theSELECTlist, the syntax check is executed in strict mode from Release 7.52.
Example
Gets all flight data for a specified departure city. The relevant airlines and flight numbers are first
passed to an internal table entry_tab, which is evaluated in the WHERE
condition of the subsequent SELECT statement. This selection could also be
carried out in a single SELECT statement by using a join in the
FROM clause. Make sure that the table entry_tab
is not initial before the SELECT statement is executed using FOR ALL ENTRIES.
DATA city TYPE spfli-cityfrom VALUE 'FRANKFURT'.
cl_demo_input=>request( CHANGING field = city ).
SELECT carrid, connid
FROM spfli
WHERE cityfrom = @( to_upper( city ) )
INTO TABLE @DATA(entry_tab).
IF entry_tab IS NOT INITIAL.
SELECT carrid, connid, fldate
FROM sflight
FOR ALL ENTRIES IN @entry_tab
WHERE carrid = @entry_tab-carrid AND
connid = @entry_tab-connid
ORDER BY PRIMARY KEY
INTO TABLE @DATA(result_tab).
cl_demo_output=>display( result_tab ).
ENDIF.
Example
Uses FOR ALL ENTRIES with an empty internal table. All rows of the database
table are respected. The number of read rows is usually, however, smaller in the first SELECT
statement than in the second statement. This is because only one column is read and hence more duplicate
rows can be removed. The second SELECT statement, on the other hand, moves all rows of the database table to the results set, since their structure covers the full table key.
DATA carriers TYPE TABLE OF scarr.
SELECT carrid, connid
FROM spfli
FOR ALL ENTRIES IN @carriers
WHERE carrid = @carriers-carrid
INTO TABLE @DATA(result1).
cl_demo_output=>write( result1 ).
SELECT carrid
FROM spfli
FOR ALL ENTRIES IN @carriers
WHERE carrid = @carriers-carrid
INTO TABLE @DATA(result2).
cl_demo_output=>display( result2 ).