Answers to your common ABAP questions
ABAP itself is not too difficult to learn. The real challenge is learning how to integrate your programs with the rest of SAP. As an SAP consultant, I get a lot of questions regarding the ins and outs of ABAP implementation. Here are just a few of the questions I see every day.
I was told not to use ON CHANGE OF in loop processing. Why?
I see this error in programs quite often. The problem is that using ON CHANGE OF does seem to work in loop processing, so many people get in the habit of using it. However, it only works the first time the code is used, and on the second pass you may get unexpected results. Look at the code sample below.
DATA: ITAB LIKE MARA OCCURS 10 WITH HEADER LINE.
ITAB-MATNR = ’5′. APPEND ITAB.
ITAB-MATNR = ’6′. APPEND ITAB.
PERFORM PRINT_ITAB.
REFRESH ITAB.
ITAB-MATNR = ’6′. APPEND ITAB.
ITAB-MATNR = ’7′. APPEND ITAB.
PERFORM PRINT_ITAB.
FORM PRINT_ITAB.
ON CHANGE OF MATNR.
WRITE: ITAB-MATNR.
ENDON.
ENDFORM
Those of you who guessed that the output would be “5 6 6 7″ are wrong! The actual output is simply “5 6 7.” What happens during the ON CHANGE OF is that SAP holds the contents of the last ON CHANGE OF variable in memory, and this does not get refreshed or cleared during loop processing. For this reason you should avoid using ON CHANGE OF when processing loops.
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.
How can I enhance performance of the SELECT statement?
I could write several complete columns on performance enhancements based only on the SELECT statement. Here are some of the worst offenders that are very easy to fix. 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.
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. SELECTing 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:
SELECT MATNR MTART FROM MARA INTO (L_MATNR, L_MTART).
… processing code …
ENDSELECT.
Another item to avoid with the SELECT statement is code like the following:
SELECT MATNR MTART FROM MARA INTO L_MATNR, L_MTART.
CHECK L_MTART <> ‘KMAT’.
… processing code …
ENDSELECT.
As in the previous SELECT * example, this will put an unnecessary load on the overall system because potentially many records that will not be processed are still going from the database server to the application server.
Is there a generic routine to initialize all fields of a structure to a certain value, regardless of how many fields are in the structure?
Welcome to field symbols! 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.
FORM INIT_STRUCTURE USING IS_STRUC IS_FILL.
FIELD-SYMBOLS: ,
.
ASSIGN (IS_STRUC) TO .
DO.
ASSIGN COMPONENT SY-INDEX OF STRUCTURE
TO .
IF SY-SUBRC NE 0. EXIT. ENDIF.
= IS_FILL.
ENDDO.
ENDFORM.
This routine is then simply called like this:
TABLES: BMM00, BMMH1.
PERFORM INIT_STRUCTURE USING: ‘BMM00′ ‘’,
‘BMMH1′ ‘’.
After the call to INIT_STRUCTURE is performed, all fields in BMM00 and BMMH1 are set to a . •
Ken Kroes has worked as an independent SAP consultant for 12 years. He is the author of The Consultant’s Guide to SAP (available as of January 1999 from Prima Publishing).
TIPS & TRICKS
• When you’re working in the ABAP editor and you want to print out just a few lines of code, use the line command PR. This works the same as the line command CC in that you put the PR at both the top line and bottom line of the section of code that you want to print and then press return. You can also use the WW command to copy sections of code to the window’s clipboard.
• Do you want to make your printed ABAP look a little better? Use the *EJECT comment in your code to force a page break on the printout.
• Have you ever had a user ask you why a certain warning or error message is coming up in a transaction? If you’re not familiar with the transaction, you’ll probably slowly debug your way in until you get to the message in question. This can be time-consuming. A faster approach is to go into debug mode when you start the transaction and then set up a break point on the keyword MESSAGE. Go to the pull down menu once you’re in the debugging screen and select BREAKPOINT-> BREAK-POINT-> KEYWORD in 3.x or BREAKPOINT-> BREAK-POINT-> STATEMENT in 4.x. Once you set the break point, press Continue and carry on with the transaction. The code will stop right where it encounters the message.
• 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 following example, the code will stop only when the userid is DRABAP. This allows other users to use the transaction without ending up in the debug screen and having to press Continue.
X = Y 1.
BREAK DRABAP.
Z = SUM / X.
Source: Unknown
———————
ABAPer, mail: abap.community@gmail.com http://www.erpdb.info