Rule Definition
As the FOR ALL ENTRIES IN is part of the WHERE clause, an empty internal table can end-up in a full table scan and thus cause performance troubles. This is why it is mandatory to include an emptiness check before any use of the FOR ALL ENTRIES IN statement.
Remediation
Use an emptiness check based on one of the following syntax :
CHECK NOT ts_table[] IS INITIAL.
or
IF NOT ts_table[] IS INITIAL.
Violation Code Sample
* Select orders for specific material and order type
SELECT matnr auart vbeln posnr
FROM vapma
INTO TABLE ts_vapma[]
WHERE matnr IN so_matnr
AND auart IN so_auart.
* Use this result to read the detail data from VBAP:
* Order Item Data
SELECT matnr vbeln kwmeng vrkme
FROM vbap
INTO TABLE ts_orders
FOR ALL ENTRIES IN ts_vapma[]
WHERE vbeln = ts_vapma
AND posnr = ts_vapma-posnr
Fixed Code Sample
* Select orders for specific material and order type
SELECT matnr auart vbeln posnr
FROM vapma
INTO TABLE ts_vapma[]
WHERE matnr IN so_matnr
AND auart IN so_auart.
* Use this result to read the detail data from VBAP:
* Order Item Data
IF NOT ts_vapma[] IS INITIAL.
SELECT matnr vbeln kwmeng vrkme
FROM vbap
INTO TABLE ts_orders
FOR ALL ENTRIES IN ts_vapma[]
WHERE vbeln = ts_vapma
AND posnr = ts_vapma-posnr.
ENDIF.
Reference
Enhancing the Quality of ABAP Development,
Meijs-Krouwels-Heulmans-Sommen, SAP Press
ISBN 1-59229-030-2
Related Technologies
Technical Criterion
CWE-391 - Unchecked Error Condition
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.