Skip to Content

Best ABAP coding practices for BW/BI consultants

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.

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