ABAP: Efficient ABAP-ing

Wednesday, February 6, 2008 | Labels: | |

I just discovered this in one of our programs in our system:

loop at i_vbel.
select vbeln into i_allo-vbeln
from vlccuorder
where vguid = i_vbel-vguid.
endselect.
append i_allo.
endloop.


For sake of simplicity, let's assume that all internal tables in my codes have its own header line (though this is not the best practice, so don't practice this).

That piece of code above is really not the best way to do it. What it intends to do is to get the latest vbeln for a specific vguid from table vlccuorder (this table is only available if you have the IS-VMS module set up in your SAP installation).

The 3 main fields in the table vlccuorder in order to get the records you need are:
vguid - Global Unique Identifier
tstmp - the timestamp
vbeln - Sales Order Number

So, if you go around it like so:

*-- to get latest vbeln for each vehicle
select tstmp vguid vbeln
from vlccuorder
into table i_all_so
for all entries in i_vbel
where vguid = i_vbel-vguid.

*-- sort so tht it is grouped by vehicles; with tstmp descending
sort i_all_so by vguid tstmp descending.
*-- get latest only
delete adjacent duplicates from i_all_so comparing vguid.


... your program will run more efficiently and easier to be understood by future programmers. SELECT ENDSELECT is really inefficient and will only hog up your system's resources (furthermore you are doing it in a loop!); unless you are VERY VERY SURE that you will end up only with one record. Like so:

select name1
from kna1
into w_name1
where kunnr eq '6000123456'.
endselect.


Why not use SELECT SINGLE?

select single name1
from kna1
into w_name1
where kunnr eq '6000123456'.


Whichever is okay for me. This matter is still open for debate though.

0 comments: