14.01.2020 Views

ABAP_to_the_Future

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Recent Code Inspector Enhancements 4.3

4.3.5 Check on Statements Following a SELECT

without ORDER BY (14/9/3)

When querying a database, you always want the queries to come back sorted by

their primary keys. Although some databases do this automatically, it’s best not to

rely on it as an assumption. Fortunately, ATC offers a check for this.

The increasingly problematic monster program now does some reads on the GT_

BEDS internal table (Listing 4.7).

SELECT *

FROM ztvc_monstr_beds

INTO CORRESPONDING FIELDS OF TABLE gt_beds

FOR ALL ENTRIES IN lt_monsters

WHERE monster_number EQ lt_monsters-monster_number.

READ TABLE gt_beds INTO gs_beds WITH KEY monster_number = 54

BINARY SEARCH.

PERFORM bed_reader.

*&--------------------------------------------------------------*

*& Form BED_READER

*&--------------------------------------------------------------*

FORM bed_reader .

READ TABLE gt_beds INTO gs_beds INDEX 1.

ENDFORM.

" BED_READER

Listing 4.7 Reading a Potentially Unsorted Table

Listing 4.7 makes an assumption that the table is sorted by the primary key. If it

is, then the BINARY SEARCH will work as expected, and reading the first record will

give you the monster with the lowest number. However, to protect yourself,

switch on the check to warn you of such problems. Figure 4.24 shows the

selected check (Search Problematic Statements for Result of a SELECT/OPEN

CURSOR without ORDER BY), and Figure 4.25 shows the results.

Figure 4.24 Check for Dodgy Statements after SELECT

199

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!