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.

Database Access 2.1

lower the sanity, the higher the ratio. Listing 2.3 shows what would have been

done prior to ABAP 7.4.

"Before

DATA: ld_converted_strength TYPE fltp,

ld_converted_sanity TYPE fltp.

SELECT monster_name monster_number strength sanity

FROM zt_monsters

INTO CORRESPONDING FIELDS OF lt_monsters.

LOOP AT lt_monsters ASSIGNING <ls_monsters>.

ld_converted_strength = <ls_monsters>-strength.

ld_converted_sanity = <ls_monsters>-sanity.

<ls_monsters>-scary_ratio =

ld_converted_strength / ld_converted_sanity.

ENDLOOP.

Listing 2.3 Operations Inside SQL Statements: Before

However, as of ABAP 7.4, it’s now po ssible to perform these operations within

the SQL query, as shown in Listing 2.4.

SELECT monster_name, monster_number

CAST( strength AS fltp ) / CAST( sanity AS fltp )

AS scary_ratio

FROM zt_monsters

WHERE monster_number = @ld_monster_number

INTO CORRESPONDING FIELDS OF @lt_monsters.

Listing 2.4 Operations Inside SQL Statements: After

As you can see, the example also included some type conversions. This saves you

from declaring two helper variables and more importantly from having to loop

over the internal table performing a calculation on each line.

Moreover, if you’re not interested in the strength and sanity on their own and

only care about the ratio, you can have two fewer fields in your internal table

(which you would have to expend extra effort to declare as “technical” in an ALV

report), and less information will be sent back from the database to the application

server (the ratio as opposed to strength and sanity). The less often such data

gets sent, the faster the program runs.

2.1.2 Buffering Improvements

One of the tasks I most enjoy is going through the ST05 trace for a program and

finding out that by making some minor changes I can take advantage of the table

85

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

Saved successfully!

Ooh no, something went wrong!