Skip to Content

This blog describes the basic best ABAP coding practices which would be helpful for BW/BI consultants.


I have written it from Start, End & field routine perspective.


Assumption:  DB_tab is database table with field names as field1 field2 field3 field4 field5.

                          itab is an internal table with field names as field1 field2 field3 field4

1) Begin End routine with


IF RESULT_PACKAGE[] IS NOT INITIAL.


This is useful because if result package does not have any value and if there is a statement like


select field1 field 2 from DB_tab into table itab FOR ALL ENTRIES IN RESULT_PACKAGE

where  field3 = <result-package>-field3 AND field4 = <result-package>-field4.

it will hit the database table to check blank values (and will fetch value against blank field3 field4 from DB table into itab. This is undesirable most of the times). Thus we can save this fruitless DB_tab hit by checking this condition.

Same is the case with Source package.


2) Do not use Select * from


  Copy only required fields (columns) from database table into internal table do not copy all database table into your internal table unless it is necessary.

3) It’s better to avoid use of into corresponding fields of

  Consider below example,


3.a Select field2 field3 field1 from DB_tab  into corresponding fields of itab WHERE ….

instead of it, use


3.b Select field1 field2 field3 from DB_tab into itab WHERE …

This means, Define itab’s columns in same order as that of database table. Also, Write into itab in same order.

In above examples performance of 3.b is much better than 3.a


4) Always use for all entries in result-package


  consider below example,


select field1 field 2 from DB_tab into table itab FOR ALL ENTRIES IN RESULT_PACKAGE

  where field3 = <result-package>-field3

     AND field4 = <result-package>-field4.


This will bring only those records from DB_tab into itab  whose fields3 & field4 values  present in result package.


This means, if result package contains only one entry with field3 = 6000 and field4 = NY

then above select statement only bring records from DB_tab with field3=6000 & field4 = NY into itab.


5) Use WHERE condition on Loop statement wherever possible.


Suppose you want to perform specific operation only for field4 = ‘LA’.


LOOP AT RESULT-PACKAGE assigning <RESULT_FIELDS> WHERE field4 = ‘LA’.

” code logic

ENDLOOP.


This can avoid useless loops for remaining values of field4.


6) Use SORT carefully.


If you have to compare field3 & field4 then sort itab by those fields only.

Use only those fields in Binary search and Delete adjucent duplicates.


  i.e   SORT itab by field3 field4.


  Then use  READ table itab into wa comparing field3 = <result_fields>-field3

                                                                                       field4 = <result_fields>-field4.

or Delete adjacent duplicates from itab comparing field3 field4.


Also, avoid use of sort on result_package as it requires considerable time to sort millions of records.


7) Don’t forget to use IF SY-SUBRC.


Always use SY-SUBRC = 0 (success) check for SELECT & READ operations to avoid any runtime errors or  unintended result values.


0 stands for success

4 stands for failure, that is no key found

8 stands for failure but it means no further keys available.

To report this post you need to login first.

51 Comments

You must be Logged on to comment or reply to a post.

  1. Arvind Tekra

    In continuation to the above practices, few additional can be taken care

    • Use of IF NOT … IS INITIAL for blank checks rather than IF … NOT IS INITIAL.
    • Use sorted or hashed tables for internal table rather than having an internal table and then sorting it.
    • The internal table should be in the same field sequence as the lookup table.
    (0) 
    1. Maik Toth

      nice blog. Always be carefull when using “for all entries”. Always check if the internal table is filled. Otherwise your performance is gone.

      (0) 
  2. Kamal Mehta

    Nice one .thanks for sharing.

    In addition can you also include use of field symobols , sorted/hashed table , select single specific to field routines etc. so that this can act as an repository for all the BW best coding practices.

    Thanks

    (0) 
  3. Mayur Priyan

    Hi,

    As an ABAPer I would like to comment a few things on the points which you have stated.

    point 1. In the IF statement to accomplish negation, ‘NOT’ must usually precede the                   logical expression.

    point 4. While using FOR ALL ENTRIES in a SELECT statement it is a must to check                 whether the internal table which you are referring to is NOT INITIAL, because if it             is initial then the SELECT statement will fetch all the entries from the Database               table.

    point 6. SORT is mandatory while using BINARY SEARCH in READ statement and                     DELETE ADJACENT DUPLICATES.

                  In Read Statement by sort it and using Binary search method the row which                  has to be fetched is retrieved faster, thus improving the programs performance.

               

                  i.e   SORT itab by field3 field4.

                  READ table itab into wa WITH KEY  field3 = <result_fields>-field3

                                                                            field4 = <result_fields>-field4.

    Cheers,

    Mayur Priyan

    (0) 

Leave a Reply