Efficient ABAP/4 Coding Techniques
7/29/2019 24efficient-abap-coding
http://slidepdf.com/reader/full/24efficient-abap-coding 1/32
Efficient ABAP/4 Coding Techniques
7/29/2019 24efficient-abap-coding
http://slidepdf.com/reader/full/24efficient-abap-coding 2/32
Efficient ABAP/4 Coding Techniques
OBJECTIVE
In this chapter we are going to learn efficient coding Techniques in
ABAP/4. The following topics are covered.
Processing Database tables
Processing internal tables Miscellaneous
String Manipulation
7/29/2019 24efficient-abap-coding
http://slidepdf.com/reader/full/24efficient-abap-coding 3/32
Efficient ABAP/4 Coding Techniques
1 Processing Database Tables
1.1 Readin g thro ugh a set of related tables
When reading through a set of related tables, use a logical database
whenever possible. The combination of defining a logical database (a
program that reads a table and its dependant table entries) and using the
logical database-related event keywords (e.g. GET) is more efficient thana set of nested SELECT ... ENDSELECT statements.
1.2 Check ing SELECT-OPTIONS when readin g v ia a Logical Database
When using the GET event keyword to retrieve records via a logicaldatabase, selection can be restricted by using the CHECK statement
(using either CHECK select-opt ion(s) or CHECK condi t ion ).
7/29/2019 24efficient-abap-coding
http://slidepdf.com/reader/full/24efficient-abap-coding 4/32
Efficient ABAP/4 Coding Techniques
In this case, a CHECK statement that returns a negative result terminates
the processing of GET events for subordinate database tables and the
associated data is not read. For efficiency reasons, you should therefore
always perform the CHECK at the highest possible level of the database
hierarchy.
For example:
SELECT-OPTIONS: S_CNTRY FOR KNA1-LAND1,S_COY FOR KNB1-BUKRS.
...
GET KNA1.
CHECK S_CNTRY.
GET KNB1.CHECK S_COY.
...
7/29/2019 24efficient-abap-coding
http://slidepdf.com/reader/full/24efficient-abap-coding 5/32
Efficient ABAP/4 Coding Techniques
is more efficient than:
SELECT-OPTIONS: S_CNTRY FOR KNA1-LAND1,
S_COY FOR KNB1-BUKRS.
...
GET KNB1.
CHECK SELECT-OPTIONS. (or CHECK: S_CNTRY,
... S_COY.)
1.3 SELECT SINGLE vs. SELECT *
SELECT SINGLE * is more efficient than SELECT * … ENDSELECT.
Whenever the full key of a table is known, use:
SELECT SINGLE * FROM dbtab WHERE …. (full key).
7/29/2019 24efficient-abap-coding
http://slidepdf.com/reader/full/24efficient-abap-coding 6/32
Efficient ABAP/4 Coding Techniques
rather than:
SELECT db_field1 db_field2 db_field3 FROM dbtabWHERE …. (full key).
...
ENDSELECT.
UP TO 1 ROW can be used to retrieve one record when the full key is
not known.
Whenever the full key is not known you will need to use the SELECT *
... ENDSELECT version of the SELECT statement.
In this case, specifying values for as many of the table’s key fields in aWHERE clause will make the SELECT statement more efficient than
checking values after the select.
7/29/2019 24efficient-abap-coding
http://slidepdf.com/reader/full/24efficient-abap-coding 7/32
Efficient ABAP/4 Coding Techniques
1.4 Selectin g via non-key fields or non-key partial fields
When selecting records from a database table when only part of a field(on which selection is based) is known, use the LIKE option as part of
the WHERE clause.
For example:
SELECT * FROM T001GWHERE BUKRS EQ ‘US01’ AND TXTKO LIKE ‘__PERS%’.
....ENDSELECT.
is more efficient than:
SELECT * FROM T001GWHERE BUKRS EQ ‘US01’.
CHECK T001G-TXTKO+2(4) = ‘PERS’. ....
ENDSELECT.
7/29/2019 24efficient-abap-coding
http://slidepdf.com/reader/full/24efficient-abap-coding 8/32
Efficient ABAP/4 Coding Techniques
1.5 SELECT <field1>, <field2> …
If you only need a few fields of a table, it is much more efficient to onlyretrieve exactly those fields from the database than to select all of them
(SELECT * …).
Example: if you only need the fields order number, order type and
customer from the sales document table, code as follows:
SELECT VBELN AUART KUNNR FROM VBAK
INTO (VBAK-VBELN, VBAK-AUART, VBAK-KUNNR)
WHERE …
WRITE: / …
ENDSELECT.
See the editor help for all the variants of the INTO clause.
7/29/2019 24efficient-abap-coding
http://slidepdf.com/reader/full/24efficient-abap-coding 9/32
Efficient ABAP/4 Coding Techniques
1.6 SELECT … UP TO n ROWS
If you only need a certain number of records specify this in your select-
statement:
SELECT … FROM … UP TO 10 ROWS.
This is much faster than issuing a SELECT without the UP TO clause and
then checking for the system variable SY-DBCNT.
1.7 SELECT INTO TABLE
Reading an internal table is faster than reading a database table.
Therefore, use internal tables to store information that must be accessed
multiple times throughout a program.
Also, use internal tables when you have to read a header - item structure
for which you would otherwise use nested SELECTs
Instead of processing a
SELECT … INTO <itab-fields>
APPEND ITAB
ENDSELECT
7/29/2019 24efficient-abap-coding
http://slidepdf.com/reader/full/24efficient-abap-coding 10/32
Efficient ABAP/4 Coding Techniques
statement it is far more efficient to select the fields straight into the
internal table and process the data from the internal table:
SELECT … FROM … INTO TABLE ITAB WHERE … LOOP AT ITAB.
… ENDLOOP.
1.8 SELECT FROM … ORDER BY
In most cases it is preferable to do the sorting within the ABAP programinstead of on the database server, that means: fill the internal table via a
SELECT statement and then sort via the SORT statement instead of
coding a SELECT … ORDER BY. The sorting of large amounts of data on
the database server affects the performance of all users on the system,
whereas the sorting within the ABAP program ‘only’ affects theapplication server. However, if an index exists on the table that can be
used for the sorting then the SELECT … ORDER BY doesn’t cause any
undue strains on the system.
7/29/2019 24efficient-abap-coding
http://slidepdf.com/reader/full/24efficient-abap-coding 11/32
Efficient ABAP/4 Coding Techniques
1.9 Agg regate Func t ions
Instead of using ABAP, some calculations can be done by using
aggregate functions for the SELECT. These are: SUM, AVG, MIN and MAX.
Example:
SELECT MATNR SUM( KWMENG ) MEINS FROM VBAP INTO TABLE
T_VBAP WHERE … GROUP BY MATNR MEINS
This example will select the cumulative sales quantities grouped by
material number and quantity unit.
1.10 SELECT with ou t WHERE
Coding a SELECT statement without a WHERE condition is only allowedfor very small tables (e.g. customizing settings). For all other tables this
is not permitted as performance problems will occur within a short period
of time.
7/29/2019 24efficient-abap-coding
http://slidepdf.com/reader/full/24efficient-abap-coding 12/32
Efficient ABAP/4 Coding Techniques
1.11 UPDATE
Instead of updating records within a SELECT … ENDSELECT construct
e.g. SELECT * FROM ZVBAK WHERE VBELN IN S_VBELN.
ZVBAK-VKBUR = W_VKBUR.
UPDATE ZVBAK.
ENDSELECT.
define your record selection in the UPDATE statement.
UPDATE ZVBAK SET VKBUR = W_VKBUR WHERE VBELN IN
S_VBELN. 1.12 DELETE
The same consideration as for the UPDATE is true for the DELETE:
Instead of deleting records within a SELECT … ENDSELECT construct
e.g. SELECT * FROM ZVBAK WHERE VBELN IN S_VBELN.
DELETE ZVBAK.
ENDSELECT.
7/29/2019 24efficient-abap-coding
http://slidepdf.com/reader/full/24efficient-abap-coding 13/32
Efficient ABAP/4 Coding Techniques
define your record selection in the DELETE statement.
DELETE FROM ZVBAK WHERE VBELN IN S_VBELN.
1.13 COMMIT
ABAP reports that issue INSERT, UPDATE or DELETE commands have to
issue COMMIT WORK statements after a logical unit of work iscompleted. Missing COMMITs can lead to bottlenecks on the database
side because the system has to keep track of the table changes via
rollback segments (in order to enable a rollback of all changes since the
last commit). Rollback segments are kept in memory and missing
COMMITs can lead to overflows of the rollback segment areas. Also the
database system holds locks on the changed records that are not
released until COMMIT time.
7/29/2019 24efficient-abap-coding
http://slidepdf.com/reader/full/24efficient-abap-coding 14/32
Efficient ABAP/4 Coding Techniques
2. Processing Internal Tables
2.1 Fill in g
To load an internal table from a database table where the structure of the
internal table is at least as wide as the structure of the database table
use:
SELECT * FROM dbtab INTO TABLE i tab.
rather than:
SELECT * FROM dbtab .
MOVE dbtab TO i tab .
APPEND i tab.
ENDSELECT.To fill an internal table without creating duplicate entries and add up the
Packed, Integer, and Floating Point fields at the same time, use:
COLLECT i tab.
7/29/2019 24efficient-abap-coding
http://slidepdf.com/reader/full/24efficient-abap-coding 15/32
Efficient ABAP/4 Coding Techniques
2.2 Sequential Reads
If you are reading through an internal table sequentially using the LOOP
at i tab ... ENDLOOP method, but are only interested in certain entries,use a WHERE clause and specify a condition for selection. Note that the
performance of a LOOP AT ... WHERE statement is improved greatly if all
fields being compared in the WHERE clause are of the same data type.
Therefore, you should try defining the ‘compare’ fields as follows:
DATA: compare_f ie ld LIKE i tab-field1.
BEGIN OF i tab OCCURS 100,
f ield1 (5), f ield2( 5),
END OF i tab.
compare_field = <value to c ompare >.
LOOP AT i tab WHERE f ield1 = compare_f ie ld .
...
ENDLOOP.
7/29/2019 24efficient-abap-coding
http://slidepdf.com/reader/full/24efficient-abap-coding 16/32
Efficient ABAP/4 Coding Techniques
2.3 Direc t Reads
If you are not processing the entire internal table, use:
READ TABLE i tab WITH KEY key BINARY SEARCH.
rather than:
READ TABLE i tab WITH KEY key
The second method performs a sequential read from the first record untilif finds a match.
The first method performs a binary search to find a matching record, but
the table must be sorted first.
7/29/2019 24efficient-abap-coding
http://slidepdf.com/reader/full/24efficient-abap-coding 17/32
Efficient ABAP/4 Coding Techniques
2.4 Sortin g
Wherever possible, identify the fields to be sorted. The format:
SORT i tab BY f ield1 field2 .
is more efficient than:
SORT i tab .
SORT i tab without fields specified attempts to sort the table by all fields
other than Packed, Integer, and Floating Point fields.
2.5 Coun ting Entr ies
To count up the number of entries in an internal table, use:
DESCRIBE TABLE i tab LINES f ield .
where f ield is a work field of type ‘I’ (integer).
7/29/2019 24efficient-abap-coding
http://slidepdf.com/reader/full/24efficient-abap-coding 18/32
Efficient ABAP/4 Coding Techniques
rather than:
LOOP AT i tab .
W_COUNT = W_COUNT + 1.ENDLOOP.
2.6 Readin g large internal tables
If you have to retrieve selected records from a large internal table, keep
this table sorted.
In this way, you can access the table via the
READ TABLE T_VBAK WITH KEY VBELN = W_VBELN BINARY SEARCH
statement.
If you only want to verify the existence of a record but don’t need any of
the fields from the record then use the additionTRANSPORTING NO FIELDS
If you only need a few fields from the internal table for processing then
use the addition
TRANSPORTING <field1> <field2> …
7/29/2019 24efficient-abap-coding
http://slidepdf.com/reader/full/24efficient-abap-coding 19/32
Efficient ABAP/4 Coding Techniques
2.7 Moving data from internal table 1 to in ternal table 2
If you need to move all entries from one internal table to another one
which has the same structure you can simply do it via the followingstatement:
ITAB2[] = ITAB1[].
2.8 Append ing data from internal table 1 to internal table 2 If you need to append records from one internal table to another one
which has the same structure you can simply do it via the following
statement:
APPEND LINES OF ITAB1 TO ITAB2.
7/29/2019 24efficient-abap-coding
http://slidepdf.com/reader/full/24efficient-abap-coding 20/32
Efficient ABAP/4 Coding Techniques
2.9 Loop ing at internal tables
If you are looping at an internal table just to count the number of records
that fulfill certain criteria then use the following variant of the loopstatement:
LOOP AT T_VBAK TRANSPORTING NO FIELDS WHERE …
ADD 1 TO COUNTER.
ENDLOOP.
The same applies if you only want to verify that at least one record exists
that satisfies a certain condition:
LOOP AT T_VBAK TRANSPORTING NO FIELDS WHERE …
EXIT.ENDLOOP.
IF SY-SUBRC = 0.
* Record found …
ENDIF.
7/29/2019 24efficient-abap-coding
http://slidepdf.com/reader/full/24efficient-abap-coding 21/32
Efficient ABAP/4 Coding Techniques
2.10 DELETING DATA FROM INTERNAL TABLES
If you need to delete a subset of records from an internal table use the
following:
DELETE T_VBAK WHERE …
2.11 DELETING DUPL ICATE ENTRIES FROM AN INTERNAL TABLE
To delete duplicate entries from an internal table the table has to be
sorted by the fields used in the comparing condition. If there is no
comparing condition the table should be sorted by all fields.
DELETE ADJACENT DUPLICATES FROM T_VBAK [COMPARING field1field2 …]
7/29/2019 24efficient-abap-coding
http://slidepdf.com/reader/full/24efficient-abap-coding 22/32
Efficient ABAP/4 Coding Techniques
3 Miscellaneous
3.1 Moving data from one table work area/struc ture to anoth er one with
an ident ical stru cture.
Use:
MOVE structure1 TO structure2 .
rather than:
MOVE-CORRESPONDING structure1 TO structure2 .
3.2 Determining the length of a str ing.
Use:
f ie ld length = STRLEN( f ield ).rather than:
IF f ield CP ‘#’.
ENDIF.
f ie ld length = SY-FDPOS.
7/29/2019 24efficient-abap-coding
http://slidepdf.com/reader/full/24efficient-abap-coding 23/32
Efficient ABAP/4 Coding Techniques
3.3 ASSIGN statement
If assigning the contants of a field belonging to a Dictionary defined
table/data structure which itself contains a name of a field to a field
symbol, use the ASSIGN TABLE FIELD syntax of the ASSIGN statement.
For example:
ASSIGN TABLE FIELD (KNA1-NAME1) TO <fs >.
is more efficient than:
ASSIGN (KNA1-NAME1) TO <FS>.
This is because the search for the field (in this case KNA1-NAME1) is
carried out only in the Data Dictionary and not in the symbol table. The
field must then be a component field of a database table declared with the
TABLES statement. This improves the performance of this statement
considerably. In contrast to the second method above, the performance
does not depend on the number of fields used within the program.
7/29/2019 24efficient-abap-coding
http://slidepdf.com/reader/full/24efficient-abap-coding 24/32
Efficient ABAP/4 Coding Techniques
3.4 Testi ng fo r Init ial Value
The use of the IF statement for checking whether a field is empty (i.e.
equal to the initial value for its data type) can be made more efficient bycomparing the field with another of the same data type.
For example:
IF MARA-IDNRA = SPACE.
....ENDIF.
is more efficient than:
IF MARA-IDNRA IS INITIAL.
....ENDIF.
7/29/2019 24efficient-abap-coding
http://slidepdf.com/reader/full/24efficient-abap-coding 25/32
Efficient ABAP/4 Coding Techniques
3.5 Testing one field fo r mult ip le values
When testing an individual field for multiple values, you can use:
IF f ield = value1 .....
ELSEIF f ield = value2. ....
ENDIF.
or:CASE f ield .
WHEN value1.
....
WHEN value2 .
....WHEN value3 .
....
WHEN valuen .
....
ENDCASE.
7/29/2019 24efficient-abap-coding
http://slidepdf.com/reader/full/24efficient-abap-coding 26/32
Efficient ABAP/4 Coding Techniques
The first method is more efficient when checking a field for up to about
five values. But the improved readability of the program code associated
with the CASE statement dictates that its use should be applied for levels
of three or greater.
3.6 Optim izing IF and CASE stru ctu res
To optimize IF and CASE structures, always test values in order of the
likelihood of each value occuring.
For example, f ieldx can have values ‘A’, ‘B’, or ‘C’. A value of ‘B’ is the
most likely value to occur, followed by ‘C’, then ‘A’. To optimize a CASE
statement for f ieldx , code the CASE statement as follows:
CASE f ieldx .
WHEN ‘B’. “Most likely value ....
WHEN ‘C’. “Next most likely value ....
WHEN ‘A’. “Least likely value ....
ENDCASE.
7/29/2019 24efficient-abap-coding
http://slidepdf.com/reader/full/24efficient-abap-coding 27/32
Efficient ABAP/4 Coding Techniques
3.7 Subrout ine / func t ion performance
Because of the added overhead of calling subroutines, functions, etc.,
you should avoid the following style of coding:
Use:
IF f ield NE 0.
PERFORM SUB1.
ENDIF.
FORM SUB1.
....
ENDFORM.
rather than:
PERFORM SUB1.
FORM SUB1.
IF f ield NE 0.....
ENDIF.
ENDFORM.
7/29/2019 24efficient-abap-coding
http://slidepdf.com/reader/full/24efficient-abap-coding 28/32
Efficient ABAP/4 Coding Techniques
3.8 Hard-cod ing Text (messages , repor t ou tpu t, etc..)
DON’T ! Wherever possible, all text to be passed to messages, or to bewritten to a report should be created as Text Symbols. This allows text to
be more easily maintained and supports the maintenance of these texts in
other languages.
3.9 Checking Return Code
The return code should always be checked after any database table
read/update statements.
For SELECT ... ENDSELECT processing loops, the return code should
be checked after the ENDSELECT statement to check for the success of
the SELECT statement.
For LOOP AT ... ENDLOOP processing loops, the return code should be
checked after the ENDLOOP statement to check for the success of the
LOOP statement.
7/29/2019 24efficient-abap-coding
http://slidepdf.com/reader/full/24efficient-abap-coding 29/32
Efficient ABAP/4 Coding Techniques
4 String manipulations
4.1 Special operators
Use the special operators CO, CA etc. instead of programming theoperation yourself. Especially on long strings, ABAP statements per
character can cause high CPU consumption.
4.2 CONCATENATE
Instead of programming a string concatenation of your own, use the
CONCATENATE statement.
Also, make use of the SPLIT statement or the STRLEN function whenever
you need to split a string into several components or have to determine
the length of a string.
4.3 Deleting leading spaces
If you want to delete the leading spaces in a string, use the ABAP
statement SHIFT … LEFT DELETING LEADING …
Avoid in any case using SHIFT inside a WHILE loop.
7/29/2019 24efficient-abap-coding
http://slidepdf.com/reader/full/24efficient-abap-coding 30/32
Efficient ABAP/4 Coding Techniques
CONCLUSION
Using these techniques we can optimize our code and increase the
performance of our program effectively.
7/29/2019 24efficient-abap-coding
http://slidepdf.com/reader/full/24efficient-abap-coding 31/32
Efficient ABAP/4 Coding Techniques
EXERCISE
1. SELECT SINGLE * is more efficient than________________________
Whenever the full key of a table is known.
2. Reading an ____________table is faster than reading a database table.
3. If you are not processing the entire internal table use _____________
4. DESCRIBE TABLE i tab LINES f ield is ued to ____________________.
5. _______________allows text to be more easily maintained and supportsthe maintenance of these texts in other languages.
6.____________ system variable used to check the return code.
7. To delete duplicate entries from an internal table__________ statement
is used.8. ABAP reports that issue INSERT, UPDATE or DELETE commands have
to issue _____________statement.
7/29/2019 24efficient-abap-coding
http://slidepdf.com/reader/full/24efficient-abap-coding 32/32
Efficient ABAP/4 Coding Techniques
SOLUTION
1. SELECT * … ENDSELECT
2. Internal table
3. BINARY SEARCH
4. to count up the number of entries in an internal table
5. TEXT SYMBOLS
6. SY-SUBRC
7. DELETE ADJACENT DUPLICATES
8. COMMIT WORK