CRITICAL
Rule Definition
The SELECT ... ENDSELECT works as a loop fetching single record for every loop pause. Basically, it works like a client cursor which will generate to much traffic on the network and communications between the application server and the database server. Having nested SELECT ... ENDSELECT is equivalent to have an SQL query inside a loops which number of iterations is based on the number of rows returned by the SQL query. Having a such a nested statement means having an SQL query inside a loop which number of iterations can be very high (dependent on the data returned from the database).
This iterative pattern has proved to be dangerous for application performance and scalability. Database servers handle in a much better set-oriented pattern rather than pure iterative ones.
Load data in internal tables instead.
Remediation
It is recommended either to modify the queries in order to use one single query using a set-oriented pattern rather than pure iterative one.
Or to use SAP ABAP internal tables instead :
"SELECT... INTO TABLE...+ LOOP AT Table" statement.
Violation Code Sample
SELECT * FROM vbak INTO wa_vbak
WHERE vbeln IN so_vbeln.
SELECT * FROM vbap INTO wa_vbap
WHERE vbeln = wa_vbak-vbeln.
SELECT SINGLE * FROM mara INTO wa_mara
WHERE matnr EQ wa_vbap-matnr.
ENDSELECT.
ENDSELECT.
Fixed Code Sample
Remediation example:
SELECT vbeln auart FROM vbak INTO TABLE ts_vbak
WHERE vbeln IN so_vbeln.
IF lines( ts_vbak ) > 0
SELECT vbeln posnr matnr kwmeng vrkme
FROM vbap
INTO TABLE ts_vbap
FOR ALL ENTRIES IN ts_vbak
WHERE vbeln = ts_vbak-vbeln.
LOOP AT ts_vbak INTO wa_vbak.
LOOP AT ts_vbap INTO wa_vbap
WHERE vbeln EQ wa_vbak-vbeln.
SELECT SINGLE * FROM mara INTO wa_mara
WHERE matnr EQ wa_vbap-matnr.
ENDLOOP.
ENDLOOP.
ENDIF.
Reference
MIT ABAP Check List
http://web.mit.edu/ist/org/admincomputing/dev/abap_review_check_list.htm
Related Technologies
Technical Criterion
CWE-1049 - Excessive Data Query Operations in a Large Data Table
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.