23-Useful Abap Tips
23-Useful Abap Tips
23-Useful Abap Tips
ABAP TIPS
USEFUL ABAP/4 INFORMATION
TIP 1
Area Use of ON CHANGE in Loop Processing
Another area to look out for in control statements for loop processing is
the use of AT. Do not use this statement when using the loop additions
FROM, TO, and WHERE. New programmers out there should remember
that AT NEW compares the structure for anything that has changed
starting at the left hand side of the structure all the way to the field that
you are specifying.
ABAP TIPS
TIP 2
Area Enhance performance of the SELECT statement
One of the most harmful things that I see nearly every day is SELECT *
FROM _ when you are using only one or two fields from the table being
read. This is usually the result of lazy coding practices. It can significantly
slow the program and put an unnecessary load on the whole system. To
understand why this is such a performance problem you have to
understand a little about SAP hardware architecture and what happens
when your ABAP processes the SELECT statement.
ABAP TIPS
When you execute an ABAP program, it runs on an application server,
which is usually a different physical box from the database server. When
you write a SELECT * statement, the application server sends the request
to the database server, which in turn must pass the entire structure for
each row back to the application server. This consumes both CPU and
networking resources, especially for tables with large structures. SELECT
ing only the specific fields you need from the database means that the
database server will only pass a small amount of data back. This will
greatly increase the speed of the SELECT. The following example outlines
this change:
TIP 3
Area Generic routine to initialize all fields of a structure
It seems that whenever you have to write a tricky piece of code, you need
a field symbol. The following code example will initialize all the fields of
any structure to any character you want. The two assumptions here are
that the structure comprises fields of the same type and that you can
move IS_FILL into them. This routine is useful when building an ABAP
program that will write a file for one of SAP's standard load programs.
Typically, the header record these programs use has a NO-DATA field that
you populate with a character you will use when you do not want SAP to
touch a field. You then fill all fields in the data record with this character,
except the ones you want SAP to process. Look at the material direct load
header record BMM00 and data record BMMH1 for an example.
ABAP TIPS
FORM INIT_STRUCTURE USING IS_STRUC IS_FILL.
FIELD-SYMBOLS: <FS_STR1>,
<FS_STR2>.
ASSIGN (IS_STRUC) TO <FS_STR2>.
DO.
ASSIGN COMPONENT SY-INDEX OF STRUCTURE <FS_STR2>
TO <FS_STR1>.
IF SY-SUBRC NE 0. EXIT. ENDIF.
<FS_STR1> = IS_FILL.
ENDDO.
ENDFORM.
TIP 4
Area Generic Tips
When you're working in the ABAP editor and you want to print out just
line command CC in that you put the PR at both the top line and bottom
window's clipboard.
Do you want to make your printed ABAP look a little better? Use the
ABAP TIPS
Have you ever had a user ask you why a certain warning or error
When you're working on a piece of code that several others are using
(a
customer exit, for example), use the
macro BREAK followed by your
user
name instead of using the BREAK-POINT ABAP keyword. In the
TIP 5
Area Database Optimizer Hints
It's true that in many cases the programmer knows exactly which index
works best, which is one reason why different database system
vendors provide so-called "optimizer hints."At first glance, it seems that
optimizer hints could make the database optimizer obsolete. But a
closer look shows that in order to select an optimal search method and
index, the programmer needs a good idea of the value distribution of
the related tables. Because R/3 is a highly configurable meta-
application used in many industries and countries by many customers,
you can find very differently scaled customer-order and order-product
relationships.
Take the following query that returns all Lufthansa flights starting at
Frankfurt:
ABAP TIPS
Copy this ABAP code into a test program, and we'll take a look at SQL
Trace - an important tool for programmers and system administrators.
You'll need to follow these steps:
ABAP TIPS
Start transaction ST05.
On the initial screen, select the trace functions you want to switch on
(SQL Trace). To switch on the trace under your user name, choose
Trace on.
If you've never tried out this function, you'll probably experience some
confusion:
ABAP TIPS
The trace list obviously contains many lines that are not related to the
SELECT statement in the ABAP program.
Explanation: The execution of any ABAP program requires additional
administrative SQL calls. To restrict the list output, you can use the
filter introducing the trace list.
The WHERE clause in the trace list's SQL statement is different from the
WHERE clause in the ABAP statement.
ABAP TIPS
Explanation: In an R/3 system, a client is a self-contained unit with
separate master records and its own set of table data (in commercial,
organizational, and technical terms). With ABAP, every Open SQL
statement automatically executes within the correct client
environment. For this reason, a condition with the actual client code
(here MANDT = '000') is added to every WHERE clause if a client field
is a component of the searched table.
Since release 4.5, you can provide optimizer hints for SELECT statements
using the %_HINTS parameter that enables emergency repairs on the
spot. Of course, this parameter only works for those database systems
that support optimizer hints. Because optimizer hints are not covered by
SQL standards, each database vendor is free to provide them. Note that
parameters starting with "%_" are never published in the documentation
and should be used with special care only in an emergency. This
introduction is very simplified; for a detailed description, please refer to
Note 129385 in OSS.
ABAP TIPS
Using the %_HINTS parameter, the example becomes:
The optimizer's execution plan for the query has changed. Instead of
performing a full table scan, the optimizer will perform an index range
scan, which is almost always a more efficient operation, unless the table
has a small number of rows.
If you specify hints incorrectly, ABAP ignores them but doesn't return a
syntax or runtime error. This lack of notification is the reason why you
have to make sure that you specified the index's correct denotation.
ABAP TIPS
TIP 6
Area Assign content to a field structure
REPORT ZFTER910.
DATA I_BLFA1 LIKE BLFA1.
FIELD-SYMBOLS: <FS>.
DO.
ASSIGN COMPONENT SY-INDEX OF STRUCTURE I_BLFA1 TO <FS>.
IF SY-SUBRC <> 0.
EXIT.
ENDIF.
<FS> = '/'.
ENDDO.
ABAP TIPS
TIP 7
Area Simple F4 selection from an internal tables
The problem: You want to select a value from an internal table within a
popup like the F4-selection?
FIELDTAB-TABNAME = 'AUSP'.
FIELDTAB-FIELDNAME = 'ATWRT'.
FIELDTAB-SELECTFLAG = 'X'.
APPEND FIELDTAB.
SELECT_VALUE = PVAR
TABLES
FIELDS = FIELDTAB
VALUETAB = VALUETAB
EXCEPTIONS
FIELD_NOT_IN_DDIC = 1
MORE_THEN_ONE_SELECTFIELD = 2
NO_SELECTFIELD = 3
OTHERS = 4.
ABAP TIPS
TIP 8
Area Tools and techniques to analyze performance problems
I will list the basics I always cover and let you develop a path that works
best for you. Rule number one is, don’t attempt to tune production
systems based on statistics from development systems or from any time
periods when data conversion, transport, or other one-time only
processes were executing.
ABAP TIPS
Here are some base-level tuning tips grouped by the five most common
tuning transactions.
1. Base configuration: servers, work processes, and load balancing. The
Process Overview (SM50, SM51, and SM66) is often the best way to gain a
high-level view of your R/3 system. With SM50 you can watch programs
execute in real time and see cumulative processing times for work
processes
Time statistics on work process activity will provide information on the
types of jobs that are being processed and on the servers handling the
heaviest loads, allowing you to quickly identify servers with too few or
too many processes.
When you see several servers with uneven distribution, review your logon
load balancing schemes
If you see work processes in PRIV mode, you need to analyze your
memory management strategies and server profile parameters. Private
mode is usually an indicator of serious or complex problems that should
be entrusted to the SAP EarlyWatch team.
ABAP TIPS
Work processes that stop or hang up will display a reason code and
probably semaphores or other valuable system codes. For semaphore
analysis, the SAP Online Support Services (OSS) Note 33873 provides a
listing of codes and definitions. Semaphores are generally wait or conflict
codes issued by the operating system.
Programs with UPDATE LOCAL will have higher database request times.
Custom programs that are not stored in the buffer and programs that
generate code (for example, Batch Data Conversion programs [BDCs] will
have higher load times).
ABAP TIPS
4. SAP R/3 buffer statistics. ST02 is used to tune system memory and
buffers. Tuning memory and buffers is very complex, so don’t attempt to
do it without extensive analysis and expertise. Additionally, memory must
always be tuned as a unit, because changes to one parameter will always
affect other areas. Several hundred profile parameters affect system
performance, and the majority of these either directly affect memory or
rely on proper memory management.
Hitratio: The hitratio is an indication of the buffers’ efficiency. When an
SAP user or system function module needs data or additional objects, the
system will generally look in R/3 buffers first. When the system is initially
started, buffers are empty; once objects are called for they are stored
subsequently in each buffer. The buffers will start with a hitratio of zero
and should improve over time to a maximum possible of 100 percent. A
system must be active for a considerable length of time before the
hitratios will be stable enough for analysis. For a stable system,
investigate buffers that consistently show hit ratios below 95 percent or
whose hitratios appear to be declining.
ABAP TIPS
Directory entries: All buffers need free directory entries. Memory space
requirements for storing directory entries is minimal, so don’t hesitate to
increase the number of directory entries for any buffer.
Roll and paging area memory: Analyze roll and paging area memory in
detail when the percentage of current use consistently exceeds 80
percent or when a large portion of processing occurs on disk rather than
in real memory.
Heap memory: Batch programs can use large amounts of heap memory
and, therefore, cause OS-level swap space requirements to increase.
When you see large amounts of heap memory being used, make sure you
have enough OS swap space to keep from freezing your operating
system.
ABAP TIPS
Call statistics: You can use call statistics to analyze table buffers and
how effectively ABAP programs use them. Investigate low hitratios and
high numbers of fails. Unusually high numbers for SELECT are often
caused by programs performing large numbers of table scans, improper
use or unavailability of appropriate indexes, or improper search
techniques.
5. SAP R/3 table statistics. Use transaction ST10 to review table call
statistics. ST10 will show the number of table changes, direct reads, and
sequential reads and the number of calls and rows affected. These
statistics can be invaluable when you’re considering buffering a table,
adding an alternate index, or stripping data and tablespaces.
Transaction DB02 shows table and index statistics. You can check for
missing indexes, files with too little or too much space, extent
problems, and a host of related items. These numbers will come in
handy when you begin stripping data and tablespaces.
Transaction DB02 shows table and index statistics. You can check for
missing indexes, files with too little or too much space, extent
problems, and a host of related items. These numbers will come in
handy when you begin stripping data and tablespaces.
Transaction DB02 shows table and index statistics. You can check for
missing indexes, files with too little or too much space, extent
problems, and a host of related items. These numbers will come in
handy when you begin stripping data and tablespaces.
TIP 9
Area SAPscript upload/download
To access the ABAP editors (as well as screen painter, menu painter)
programmatically; use function: RS_TOOL_ACCESS.
ABAP TIPS
TIP 12
Area Hiding ABAP source code
In SAP releases upto 4.6C you can place the special character sequence
*@#@@[SAP] on the first line of an ABAP to hide the source code - it will
still run and behave as normal. To bring the code back to visible you must
upload it again from a file or put it into a Transport - release it, then edit
the transport file and remove the character string.
For 4.6C you can look at table: D010SINF This table includes a field
SQLX. When this field is set to "X" the program source will not be
viewable as for program: SAPMSYST.
ABAP TIPS
TIP 13
Area Delivery due list for release 4.6
The delivery due list (VL10G) uses user-exits to enable extra data to be
presented to the user in the ALV list - these are:
LV50R_VIEWG03 for data from Sales and LV50R_VIEWG06 for data from
Purchasing.
ABAP TIPS
TIP 14
Area To change development class of a SAPscript
TIP 17
Area Calling an ABAP form from SAPscript
In Layout Set:
DEFINE &X& = ...
DEFINE &Y& = ...
DEFINE &Z& = ...
PERFORM XXXXXX IN Zxxxxxxx
USING &X&
USING &Y&
CHANGING &Z&
You can debug a SAPscript: Use Tools - Word Processing - Layout Set.
Enter name of layout set and then Utilities - Activate Debugger.
TIP 19
Area Standard Conversion Exits
After the first time a program has been transported, there are two ways to
move a variant. The first method is to manually add an entry to the
transport for the variant you want to move. The format of the entry is
LIMU VARX xxxxxxxxName_of_the_variant where xxxxxxxx is the
program name.
The last method is the easiest, in that you do not have to remember any
arcane codes. Go to the ABAP editor, and go to the variant screen.
Under the Utilities menu is Transport Variant. This allows you to choose
the variants to transport, and the transport to put them in.
ABAP TIPS
TIP 21
Area List of methods to FTP from within SAP
NOTE: ftp can accept commands from a file, and it is often useful to
create a file containing the commands, and pipe the file into the ftp
command.
ABAP TIPS
TIP 22
Area User Status Tip
If you have several status' for a program that are nearly identical, it can be
easier to setup and maintain a single status, and just hide parts of it as
required.
EG:
*$*$* - By placing *$*$ at the beginning of a comment line will lock the line
for editing. You are able to edit the line until you hit the enter key.
ABAP TIPS
TIP 23
Area Change the font on the X_65_255 paper size
This tip was taken from the SAP-R3 mailing list, and is by Sheila Tichener.
If you need to use a large line-size to get all your data in I know the sap
version of X_65_255 for hp laser jet 4 gives an unnecessarily small font .
You can change it in SPAD (or copy to Y_65_255) as follows.
Choose Device formats, Change
hplj4
x_65_255
Execute
Double click on printer initialization
Change from:-
# select Courier 16.67 CPI normal font
\e(s0p16.67h0s0b4099T
to:-
# select Arial 24 CPI 15 point normal
\e(s0p24h15v0s0b16602T
ABAP TIPS
TIP 24
Area To find a single quote within a string
Use 4 single quotes to find one quote in a string. The following code
snippet will replace a single quote with a double quote. replace '''' with '"'
into field
ABAP TIPS
TIP 25
Area Helping the DBMS find the correct index
Sometime, you may find that the a SELECT that you have coded is very
slow, even though you have put all the fields needed for an index in
your where clause. Usually, this means that the DBMS is selecting the
wrong index, you can tell if this is the case by doing an SQL trace
(System - Utilities - SQL Trace). You can help the DBMS select the
correct index by providing hints, include all fields from the index, and
make sure that the fields are in the same order as they appear in the
index.
ABAP TIPS
TIP 26
Area Saving screens created with Screen Painter
You can not download code for the Screens (except for the ABAP part),
but at least you can download a printout of what you set up. If you are in
Object Browser looking at the module program, you can highlight the
screens and click the print button. Click no to print immediately, then go
into System -- Services -- Output Control to select the spool request and
then go into List -- Save -- Local File. (Of course, you can send it to a
printer to see documentation on your screen).
ABAP TIPS
TIP 27
Area Help Views
Example: