You are on page 1of 5

General Guidelines for ABAP coding in BW Mohan Kotwal Business Card Company: Bradford & Galt, Inc.

Posted on Jun. 09, 2011 06:24 AM in Enterprise Data Warehousing/Business Warehouse


General guidelines for ABAP coding in BW: Business Case:

Subscribe Print Permalink Share

ABAP coding is very widely used in any BW implementation for writing routines in transformation. Bad ABAP code written in transformation can adversely affect the data accuracy as well as data loading performance. This document is prepared for SAP BW developer to follow some best practices while writing ABAP code in BW. By following these best practices, you will be able to achieve best data loading performance as well as data accuracy. 1) When to use Start Routine: Make use of Start Routine for the scenarios listed below

Processing all the data packet records at once. For example, to delete records or update records with new value. To acquire the runtime information of a data transfer process (DTP). To populate internal table and utilize in field level routine.

2) How to define Internal Table: 1. 1. 2. 3. 4. 5. Example: TYPES: BEGIN OF TYPE_<name>, . END OF TYPE_<name>. Define TYPE structure. Define Internal table referring TYPE defined above. Define Internal table as Hashed table or Sorted table with keys. Define Internal table without header line. Try not to use OCCURS clause because of potential problem in ABAP OO programs.

DATA: T_<name> TYPE SORTED TABLE OF TYPE_<name> WITH NON-UNIQUE KEY <key1> <key2> Or DATA: T_<name> TYPE HASHED TABLE OF TYPE_<name> WITH UNIQUE KEY <key1> <key2>

3) How to Read Internal Table:

Always read Internal table using ASSIGNING Field Symbol unless there is a need for the Work Area.

Example: READ TABLE <itab> ASSIGNING <field_symbol> WITH TABLE KEY <key1> = .. <key2> = ... 4) How to loop on SOURCE_PACKAGE :

Always use Field Symbols while looping on SOURCE_PACKAGE unless there is a need for the Work Area. Always use Field Symbols to modify SOURCE_PACKAGE field content.

Example: LOOP AT SOURCE_PACKAGE assigning <field_symbol>. <field_symbol>-MATERIAL = . ENDLOOP. 5) Try to avoid:


Avoid SELECT ENDSELECT. Avoid multiple SELECT statements on the same table.

Avoid SELECT * from <TABLE> statement. Instead specify the explicit fields like SELECT <fld1> <fld2> <fld3> from <TABLE>. Only use SELECT * or SELECT SINGLE * when all the fields are needed otherwise specify the fields individually.

Example: SELECT EBELN EBELP MATNR INTO CORRESPONDING FIELDS OF TABLE <itab> FROM EKPO.

Avoid SELECT statement inside LOOP ENDLOOP. Instead it is always advisable to collect all the data in an internal table before starting LOOP ENDLOOP and then read that internal table inside LOOPENDLOOP.

Example: SELECT EBELN EBELP MATNR INTO CORRESPONDING FIELDS OF TABLE itab FROM EKPO. LOOP AT SOURCE_PACKAGE assigning <SOURCE_FIELDS>. READ TABLE itab ASSIGNING <l_itab> WITH TABLE KEY <key1> = . <key2> = . ENDLOOP.

Avoid SELECT inside field level routine. Instead it is always advisable to collect all the data in an internal table in the start routine and then read that internal table inside field level routine.

6) Important DON'Ts

DO NOT include the statement BREAK-POINT in any code sent to production. DO NOT directly update, insert or delete records from SAP tables such as Master data table or DSO Active table.

DO NOT directly use text literals (such as P, BASIS, etc). Instead declare them as constants in the global area of the start routine.

7) Points to be considered for the optimal data loading performance:


Use Hash Table or Sorted Table. Internal Table read should use table key or binary search. To define Internal Table: No data: begin of. Use type: begin of. Do not use Loop at <itab> Endloop in place of READ <itab>. Loop at <itab> where should be performed on sorted tables using the defined keys if possible. Only loop at assigning <field-symbol> should be used unless there is a need for the work area. Only read assigning <field-symbol> should be used unless there is a need for the work area. Inserts should be used on sorted and hashed tables instead of appends. Follow the points mentioned in the item 5 Try to avoid above. Use JOIN to select multiple tables with same parameters. Check if record already exists in the internal table before executing SELECT or calling function module:

It is a best practice to store data selected from database table into an internal table and read that internal table to verify if record with similar combination does exist in that internal table before executing SELECT or calling function module. This way you can avoid duplicate selects on the database and improve the data loading performance. Example: READ <itab> assigning <field-symbol> WITH TABLE KEY EBELN = . EBELP = .. IF SY_SUBRC <> 0. SELECT SINGLE EBELN EBELP MATNR FROM EKPO INTO CORRESPONDING FIELDS OF TABLE <itab> . WHERE EBELN = AND EBELP = ENDIF.

When you use FOR ALL ENTRIES IN statement in SELECT, do not forget to check if reference internal table is not initial.

Example: IF <reference internal table> IS NOT INITIAL. SELECT EBELN EBELP MATNR INTO CORRESPONDING FIELDS OF TABLE <itab> FROM <table> FOR ALL ENTRIENS IN <reference internal table> WHERE ENDIF.

8) Points to be considered from data accuracy stand point:


Refresh internal table before populating it again. Clear all Variables and Work Areas before populating them again. As a good practice try to obtain only Active versions from the InfoObject (OBJVERS = A). While dividing any value, check if Denominator is not zero.

Example: IF <denominator> NE 0. <result> = <Numerator> / <Denominator>. EDNIF.

You might also like