Efficient Abap4 Coding Techniques
Efficient Abap4 Coding Techniques
OBJECTIVE
In this chapter we are going to learn efficient coding Techniques in
ABAP/4. The following topics are covered.
M iscellaneous
String Manipulation
When using the GET event keyword to retrieve records via a logical
database, selection can be restricted by using the CHECK statement
(using either CHECK select -opt ion (s) or CHECK condition).
For example:
SELECT-OPTIONS: S_CNTRY FOR KNA1-LAND1,
S_COY FOR KNB1-BUKRS.
...
GET KNA1.
CHECK S_CNTRY.
GET KNB1.
CHECK S_COY
GET KNB1.
CHECK SELECT-OPTIONS. (or CHECK: S_CNTRY,
...
S_COY.)
rather than:
•
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 a WHERE clause will make the SELECT
statement more efficient than checking values after the select.
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 T001G
WHERE BUKRS EQ ‘US01’
AND TXTKO LIKE ‘__PERS%’.
....
ENDSELECT.
is more efficient than:
SELECT * FROM T001G
WHERE BUKRS EQ ‘US01’.
CHECK T001G-TXTKO+2(4) = ‘PERS’.
....
ENDSELECT.
If you only need a few fields of a table, it is much more efficient to only retrieve 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:
ENDSELECT.
See the editor help for all the variants of the INTO clause.
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.
statement it is far more efficient to select the fields straight into the internal table and process the data
from the internal table:
Example:
SELECT MATNR SUM( KWMENG ) MEINS FROM VBAP INTO TABLE
T_VBAP WHERE … GROUP BY MATNR MEINS
ENDSELECT.
1.12 DELETE
The same consideration as for the UPDATE is true for the DELETE:
1.13 COMMIT
2.1 Filling
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:
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 itab .
If you are reading through an internal table sequentially using the LOOP at itab ... 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:
rather than:
LOOP AT itab.
W_COUNT = W_COUNT + 1.
ENDLOOP.
If you have to retrieve selected records from a large internal table, keep
this table sorted.
TRANSPORTING NO FIELDS
If you only need a few fields from the internal table for processing then
use the addition
TRANSPORTING <field1> <field2> …
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 following statement:
ITAB2[] = ITAB1[].
2.8 Appending 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:
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 loop
statement:
IF SY-SUBRC = 0.
* Record found …
ENDIF.
If you need to delete a subset of records from an internal table use the
following:
3.1 Moving data from one table work area/structure to another one with an identical structure.
Use:
MOVE structure1 TO structure2.
rather than:
MOVE-CORRESPONDING structure 1 TO structure2.
Use:
fieldlength= STRLEN( field).
rather than:
IF fie ld CP ‘#’.
ENDIF.
fieldlength= SY-FDPOS.
For example:
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.
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 by
comparing 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
When testing an individual field for multiple values, you can use:
IF field = value1.
....
ELSEIF f ield = value2.
....
ENDIF.
or:
CASE field.
WHENvalue1.
....
WHENvalue2.
....
WHENvalue3.
....
WHEN value n.
....
ENDCASE.
The first (IF….ENDIF) 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.
For example, field x 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 fie ld x, code the CASE statement as
follows:
CASE field x.
WHEN ‘B’.
“Most likely value
....
WHEN ‘C’.
“Next most likely value
....
WHEN ‘A’.
“Least likely value
....
ENDCASE.
FORM SUB1.
....
ENDFORM.
rather than:
PERFORM SUB1.
FORM SUB1.
IFfield NE 0.
....
ENDIF.
ENDFORM.
DON’T !
Wherever possible, all text to be passed to messages, or to be written 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.
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.
4 String manipulations
String manipulations
4.2 CONCATENATE
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.
CONCLUSION
Using these techniques we can optimize our code and increase the
performance of our program effectively.
EXERCISE
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