Skip to Content
Author's profile photo Harshawardhan Ghatge

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.

Assigned Tags

      51 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo CH Raman
      CH Raman

      Really useful one. Thanks for sharing with us Harsha.

      Regards

      Raman

      Author's profile photo Harshawardhan Ghatge
      Harshawardhan Ghatge
      Blog Post Author

      Thank you Raman.

      Author's profile photo Former Member
      Former Member

      Very nice Document

      Regards,

      Sushant

      Author's profile photo Harshawardhan Ghatge
      Harshawardhan Ghatge
      Blog Post Author

      Thanks Sushant.

      Author's profile photo Former Member
      Former Member

      very clear points to improve coding standards and performance,

      nice one harshawardhan...

      thank u for sharing ....

      Author's profile photo Harshawardhan Ghatge
      Harshawardhan Ghatge
      Blog Post Author

      Thanks Vasudev.. you are welcome..

      Author's profile photo Anshu Lilhori
      Anshu Lilhori

      Really helpful..

      Regards,

      AL

      Author's profile photo Harshawardhan Ghatge
      Harshawardhan Ghatge
      Blog Post Author

      thanks Anshu..

      Author's profile photo Former Member
      Former Member

      Very useful blog Harshawardhan....

      Author's profile photo Harshawardhan Ghatge
      Harshawardhan Ghatge
      Blog Post Author

      Thanks a lot Jatin ..

      Author's profile photo Former Member
      Former Member

      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.
      Author's profile photo Harshawardhan Ghatge
      Harshawardhan Ghatge
      Blog Post Author

      Thank you Arvind for your suggestions.

      Author's profile photo Maik Toth
      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.

      Author's profile photo Former Member
      Former Member

      Nice Document, Thanks for sharing!

      Author's profile photo Harshawardhan Ghatge
      Harshawardhan Ghatge
      Blog Post Author

      Thanks Hema

      Author's profile photo Aravind Nag M
      Aravind Nag M

      Nice blog...

      Author's profile photo Harshawardhan Ghatge
      Harshawardhan Ghatge
      Blog Post Author

      thanks Arvind

      Author's profile photo Former Member
      Former Member

      Thanks for sharing !! 🙂

      Author's profile photo Harshawardhan Ghatge
      Harshawardhan Ghatge
      Blog Post Author

      Thanks Akber

      Author's profile photo praveen kumar V
      praveen kumar V

      Thanks Harsha very  useful document

      Author's profile photo Harshawardhan Ghatge
      Harshawardhan Ghatge
      Blog Post Author

      Thanks Praveen

      Author's profile photo Former Member
      Former Member

      Hi,

      a good and clear document.

      Thanks for sharing.

      Observation

      about 5) point , please correct this: LOOP AT RESULT-PACAKGE

      Cheers

      Ivan

      Author's profile photo Harshawardhan Ghatge
      Harshawardhan Ghatge
      Blog Post Author

      Thanks Ivan. Corrected.

      Author's profile photo Former Member
      Former Member

      Hi

      Nice and Good document

      Thanks,

      Purushotham.

      Author's profile photo Harshawardhan Ghatge
      Harshawardhan Ghatge
      Blog Post Author

      Thanks Purushotham. Can you rate it ?

      Author's profile photo Krishna Chaitanya
      Krishna Chaitanya

      Good Job.. Thanks for sharing.

      🙂

      Regards,

      Krishna Chaitanya

      Author's profile photo Harshawardhan Ghatge
      Harshawardhan Ghatge
      Blog Post Author

      Thanks Krishna. Can you rate it.

      Author's profile photo Krishna Chaitanya
      Krishna Chaitanya

      Hi Harshawardhan,

      I think you got my rating.....right?

      Regards,

      Krishna Chaitanya.

      Author's profile photo Former Member
      Former Member

      Thanks a lot for sharing this 🙂

      Being new to ABAP these tips will surely help a lot in learning and improving the code 🙂

      Regards,

      Priyanka

      Author's profile photo Harshawardhan Ghatge
      Harshawardhan Ghatge
      Blog Post Author

      Thanks Priyanka...!! 🙂

      Author's profile photo Former Member
      Former Member

      Good effort and  explained clearly. Thanks for sharing

      Author's profile photo Harshawardhan Ghatge
      Harshawardhan Ghatge
      Blog Post Author

      Thanks Priyanka..

      Author's profile photo Former Member
      Former Member

      Thanks for compiling the important points of the routines. It's a nice document and extremely helpful.

      Regards,

      Prasad

      Author's profile photo Harshawardhan Ghatge
      Harshawardhan Ghatge
      Blog Post Author

      Thanks Prasad

      Author's profile photo Kamal Mehta
      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

      Author's profile photo Harshawardhan Ghatge
      Harshawardhan Ghatge
      Blog Post Author

      Thanks Kamal. I will surely try to consider your suggetions.

      Author's profile photo Former Member
      Former Member

      Very useful... thanks! 🙂

      Author's profile photo Harshawardhan Ghatge
      Harshawardhan Ghatge
      Blog Post Author

      Thanks a lot Rohini..!

      Author's profile photo Former Member
      Former Member

      Good one, Thanks for sharing....

      Author's profile photo Harshawardhan Ghatge
      Harshawardhan Ghatge
      Blog Post Author

      Thank you Mithun..

      Author's profile photo Mayur Priyan
      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

      Author's profile photo Harshawardhan Ghatge
      Harshawardhan Ghatge
      Blog Post Author

      Thanks Mayur for your words..!!

      Author's profile photo Martin Grob
      Martin Grob

      thanks for sharing nicely done work

      Author's profile photo Harshawardhan Ghatge
      Harshawardhan Ghatge
      Blog Post Author

      Thank you Martin..

      Author's profile photo Former Member
      Former Member

      Useful one .Harsha Thanks for sharing

      Author's profile photo Harshawardhan Ghatge
      Harshawardhan Ghatge
      Blog Post Author

      Thanks Ranbir..

      Author's profile photo Former Member
      Former Member

      Thanks Harsha for this helpful document.

      Author's profile photo Harshawardhan Ghatge
      Harshawardhan Ghatge
      Blog Post Author

      Thanks Susheeth..

      Author's profile photo Former Member
      Former Member

      amazing Document..Its very easy to Understand..

      Keep Blogging

      Author's profile photo Prakash K
      Prakash K

      Nice efforts to simply the things..

      BR,

      Prakash

      Author's profile photo Former Member
      Former Member

      Simple and yet useful information.

      Thank you.