ABAP Tips and Tricks : More
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.
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
If you like this post, you may as well like these too:
- ABAP Tips and Tricks #3: Programming Tips When one uses the MOVE statement, try keep the destination operands as the same data type as the ‘from’ operands. Use the FREE <table> command once the program has completed...
- ALL ABAP Tips & Tricks http://rapidshare.com/files/59540170/ABAP.hlp...
- ABAP Tips and Tricks #2: Generic Hints Initialization Event: This event is executed before the output of the selection screen. The INITIALIZATION section is only performed when the ABAP is started directly, and is not executed if...
- ABAP Tips and Tricks #1: Generic Hints Use the CHECK command instead of an IF statement whenever possible. This reduces processing time and improves readability. Use the ON CHANGE OF command instead of developing code to compare...
- ABAP Tips & Tricks : Data Dictionary Maintenance Views Lock objects Change database table entry even if it is not allowed ~~~~~End of Post~~~~~ ——————— ABAPer, mail: abap.community@gmail.com http://www.erpdb.info...
- SAP HR Tips and Tricks Here is a huge collection of Tips and Tricks on SAP HR. Understand more about Payroll, Organisation structure, HR Objects, Staffing and more in this Manual....
- Sales and Distribution Tips and Tricks Here are few documents on SAP SD Tips and Tricks. This contains : Assigning number ranges for different customer account groups Changing the text Sales Order to Billing Request in...
- Production Planning Tips and Tricks Contains: Convert Planned Orders to Purchase requisitions and Purchase Orders Difference between Co-Product and Bi-Product with an example Implementing Screen Exit for the transaction CO01 Production Order Splitting Repetitive Manufacturing...
- Collection of Material Master Tips and Tricks So, what does this bundle contain? How to calculating total quantity supplied by a vendor in a particular period? How to check duplicates for the incoming invoices? Configuring stock transfer...
- SAP Production Planning Tips and Tricks SAP Production Planning Tips and Tricks contains a huge collections of smart answers to get the work done faster. Here is the list of tips this manual provides. Download the...
- ABAP Programming Tips This is a very vast collection of ABAP Programming Tips for download. Download ABAP Programming Tips from Rapidshare...
- ABAP Optimisation tips Download Here Contributed by Rajagopalan M. ——————— ABAPer, mail: abap.community@gmail.com http://www.erpdb.info...
- ABAP Tips : Programming ABAP Language Language Dependent Formatting of Amount and Currency Dynamic Programming Assign Variable Type at Runtime Dynamic SQL Dynamic program generation Dynamic Tokens Field symbols Dynamically read fields, fieldnames and...
- ABAP Tips: Reporting and Selection-screen ABAP Reporting Report Template Calling another report from your own report Calling a dialog screen from a report and passing data Call dialog screen from report – Example 2 Control...
- ABAP Tips on Data Dictionary and Internal tables Data Dictionary Maintenance Views Lock Objects Internal Tables Free text search in a field in a internal table Types of Internal Tables Working with Internal Tables...



















Wow…. great post. Keep up the good work man.