Avoid using FOR ALL ENTRIES IN without emptiness check on XXL Tables | CAST Appmarq

Avoid using FOR ALL ENTRIES IN without emptiness check on XXL Tables


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 very important performance troubles as this metric applies only to extremely large tables (XXL tables). 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

Health Factor

  Efficiency


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.

Benchmark Statistics

Global Compliance

90.83%

Total Violations
10,927
Total Opportunities
119,207
Average Violations / App.
106.09
The compliance score represents 1 minus the ratio between the number of times a rule has been violated compared to the number of opportunities in a set of applications that the rule could have been violated.

Industry Insights

Manufacturing

93.84%

Retail

89.02%

Energy

84.19%