23-Useful Abap Tips

Click here to load reader

  • date post

    14-Nov-2014
  • Category

    Documents

  • view

    129
  • download

    10

Embed Size (px)

Transcript of 23-Useful Abap Tips

ABAP TIPS

ABAP TIPSUSEFUL ABAP/4 INFORMATION TIP Area

1Use of ON CHANGE in Loop Processing

ON CHANGE OF does seem to work in loop processing.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.

ABAP TIPSDATA: 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

ABAP TIPSThose 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.

ABAP TIPSTIP Area

2Enhance 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 TIPSWhen 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: SELECT MATNR MTART FROM MARA INTO (L_MATNR, L_MTART). ... processing code ... ENDSELECT.

ABAP TIPSAnother 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 8server to the application server.

ABAP TIPSTIP Area

3Generic 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 TIPSFORM 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 \.

ABAP TIPSTIP Area

4Generic Tips

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 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

ABAP TIPS 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->

ABAP TIPS 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 user-id is DRABAP. This allows other users to use the transaction without ending up in the debug screen and having to press Continue. X = Y1. BREAK DRABAP. Z = SUM / X.

ABAP TIPSTIP Area 5 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 metaapplication 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 TIPSDATA: xcarrid LIKE spfli-carrid, xconnid LIKE spfli-connid, xcityfrom LIKE spfli-cityfrom. SELECT carrid connid cityfrom FROM spfli INTO (xcarrid, xconnid, xcityfrom) WHERE carrid = 'LH ' AND cityfrom ='FRANKFURT'. WRITE: / xcarrid, xconnid, xcityfrom. ENDSELECT. 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 TIPSStart 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. Run the test program you just created. Select the trace functions you want to switch off. Choose Trace off. Now the system has recorded all the SQL calls being executed during this time. Choose Trace list to get a list of the calls. If you've never tried out this function, you'll probably experience some confusion:

ABAP TIPSThe 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 trace list contains different SQL statements simultaneously related to the one SELECT statement in the ABAP program. Explanation: The R/3 Database Interface - a sophisticated component of the R/3 Application Server - maps every Open SQL statement to one or a series of physical database calls and brings it to execution. This mapping, crucial to R/3s performance, depends on the particular call and database system. The WHERE clause in the trace list's SQL statement is different from the WHERE clause in the ABAP statement.

ABAP TIPSExplanation: 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 TIPSUsing the %_HINTS parameter, the example becomes: SELECT carrid connid cityfrom FROM spfli INTO (xcarrid, xconnid, xcityfrom) WHERE carrid = 'LH ' AND cityfrom = 'FRANKFURT' %_HINTS ORACLE 'INDEX("SPFLI" "SPFLI~001")'. WRITE: / xcarrid, xconnid, xcityfrom. ENDSELECT. 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 ope