Professional Documents
Culture Documents
COM
ABAP TRICKS and TIPS
SQL INTERFACE
VS
Always specify your conditions in the Where-clause instead of checking them yourself with check
statements. The database system can then use an index (if possible) and the network load is considerably
less.
VS
For all frequently used Select statements, try to use an index. You always use an index if you specify (a
generic part of) the index fields concatenated with logical Ands in the Select statement's Where clause.
Note that complex Where clauses are poison for the statement optimizer in any database system.
LOGON TO ABAPLOVERS.BLOGSPOT.COM
ABAPLOVERS.BLOGSPOT.COM
ABAP TRICKS and TIPS
VS
If you are interested if there exists at least one row of a database table or view with a certain condition, use
the Select Single statement instead of a Select-Endselect-loop. Select Single requires one communication
with the database system, whereas Select-Endselect needs two.
VS
It is always faster to use the Into Table version of a Select statement than to use Append statements.
LOGON TO ABAPLOVERS.BLOGSPOT.COM
ABAPLOVERS.BLOGSPOT.COM
ABAP TRICKS and TIPS
Select aggregates
C4A = '000'.
SELECT * FROM T100 INTO T100_WA
WHERE SPRSL = 'D' AND
ARBGB = '00'.
CHECK: T100_WA-MSGNR > C4A.
C4A = T100_WA-MSGNR.
ENDSELECT.
VS
If you want to find the maximum, minimum, sum and average value or the count of a database column, use a
select list with aggregate functions instead of computing the aggregates yourself. Network load is
considerably less.
VS
If you process your data only once, use a Select-Endselect-loop instead of collecting data in an internal
table with Select Into Table. Internal table handling takes up much more space.
LOGON TO ABAPLOVERS.BLOGSPOT.COM
ABAPLOVERS.BLOGSPOT.COM
ABAP TRICKS and TIPS
VS
To process a join, use a view instead of nested Select statements. Network load is considerably less.
VS
SELECT * INTO WA
FROM SPFLI AS P INNER JOIN SFLIGHT AS F
ON P~CARRID = F~CARRID AND
P~CONNID = F~CONNID.
ENDSELECT.
To read data from several logically connected tables use a join instead of nested Select statements. Network
load is considerably less.
LOGON TO ABAPLOVERS.BLOGSPOT.COM
ABAPLOVERS.BLOGSPOT.COM
ABAP TRICKS and TIPS
VS
Use a select list or a view instead of Select * , if you are only interested in specific columns of the table.
Network load is considerably less.
VS
For all frequently used, read-only tables, try to use SAP buffering. Network load is considerably less.
LOGON TO ABAPLOVERS.BLOGSPOT.COM
ABAPLOVERS.BLOGSPOT.COM
ABAP TRICKS and TIPS
VS
Whenever possible, use array operations instead of single-row operations to modify your database tables.
Frequent communication between the application program and database system produces considerable
overhead.
Column Update
VS
UPDATE SFLIGHT
SET SEATSOCC = SEATSOCC – 1.
Whenever possible, use column updates instead of single-row updates to update your database tables.
Network load is considerably less.
LOGON TO ABAPLOVERS.BLOGSPOT.COM
ABAPLOVERS.BLOGSPOT.COM
ABAP TRICKS and TIPS
Using Subqueries
VS
Instead of using nested Select loops or FOR ALL ENTRIES it is often possible to use subqueries. Network
load is considerably less.
LOGON TO ABAPLOVERS.BLOGSPOT.COM
ABAPLOVERS.BLOGSPOT.COM
ABAP TRICKS and TIPS
String Manupulations
DO 200 TIMES.
IF <C1A> = '(' OR <C1A> = ')'.
"...any actions
EXIT.
ENDIF.
ASSIGN <C1A>+1 TO <C1A>.
ENDDO.
VS
IF CHA(200) CA '()'.
"...any actions
ENDIF.
Use the special operators CO, CA, CS, instead of programming the operations yourself. If ABAP/4
statements are executed per character on long strings, CPU consumption can rise substantially.
LOGON TO ABAPLOVERS.BLOGSPOT.COM
ABAPLOVERS.BLOGSPOT.COM
ABAP TRICKS and TIPS
String Concatenation
VS
CONCATENATE T100_WA-ARBGB
T100_WA-MSGNR
T100_WA-TEXT INTO CLA.
Some function modules for string manipulation have become obsolete and should be replaced by ABAP/4
statements or functions: STRING_CONCATENATE... -> CONCATENATE, STRING_SPLIT... -> SPLIT,
STRING_LENGTH -> strlen(), STRING_CENTER -> WRITE...TO...CENTERED, STRING_MOVE_RIGHT ->
WRITE...TO...RIGHT-JUSTIFIED
________________________________________________________________________
LOGON TO ABAPLOVERS.BLOGSPOT.COM
ABAPLOVERS.BLOGSPOT.COM
ABAP TRICKS and TIPS
String Concatenation
* Mrs. Jane Miller from New York City" is the final value of CHA.
VS
CONCATENATE
'Mrs.' CMA CMB 'from' CMC INTO CHA
SEPARATED BY SPACE.
* Mrs. Jane Miller from New York City" is the final value of CHA.
Use the CONCATENATE statement instead of programming a string concatenation of your own.
LOGON TO ABAPLOVERS.BLOGSPOT.COM
ABAPLOVERS.BLOGSPOT.COM
ABAP TRICKS and TIPS
VS
If you want to delete the leading spaces in a string, use the ABAP/4 statement SHIFT...LEFT DELETING
LEADING... .Other constructions (with CN and SHIFT...BY SY-FDPOS PLACES, with CONDENSE if
possible, with CN and ASSIGN CLA+SY-FDPOS(LEN) ...) are not as fast. In any case, avoid using SHIFT
inside a WHILE-loop!
LOGON TO ABAPLOVERS.BLOGSPOT.COM
ABAPLOVERS.BLOGSPOT.COM
ABAP TRICKS and TIPS
String Split
* CMA contains '(410)-45174-66354312' and shall be
* split into AREA_CODE,
* TEL_NO1,
* TEL_NO2.
SEARCH CMA FOR '-'.
MOVE CMA(SY-FDPOS) TO AREA_CODE.
I1 = SY-FDPOS + 2.
SEARCH CMA FOR '-' STARTING AT I1.
I1 = I1 - 1.
MOVE CMA+I1(SY-FDPOS) TO TEL_NO1.
I1 = I1 + SY-FDPOS + 1.
MOVE CMA+I1 TO TEL_NO2.
VS
* CMA contains '(410)-45174-66354312' and shall be
* split into AREA_CODE,
* TEL_NO1,
* TEL_NO2.
LOGON TO ABAPLOVERS.BLOGSPOT.COM
ABAPLOVERS.BLOGSPOT.COM
ABAP TRICKS and TIPS
I1 = STRLEN( CLA ).
Use the strlen( ) function to restrict the DO loop to the relevant part of the field, e.g. when determinating a
check-sum.
LOGON TO ABAPLOVERS.BLOGSPOT.COM
ABAPLOVERS.BLOGSPOT.COM
ABAP TRICKS and TIPS
CLEAR STRING.
TRANSLATE STRING USING ' *'.
VS
Use "CLEAR f WITH val" whenever you want to initialize a field with a value different from the field's type-
specific initial value.
LOGON TO ABAPLOVERS.BLOGSPOT.COM