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.
Really useful one. Thanks for sharing with us Harsha.
Regards
Raman
Thank you Raman.
Very nice Document
Regards,
Sushant
Thanks Sushant.
very clear points to improve coding standards and performance,
nice one harshawardhan...
thank u for sharing ....
Thanks Vasudev.. you are welcome..
Really helpful..
Regards,
AL
thanks Anshu..
Very useful blog Harshawardhan....
Thanks a lot Jatin ..
In continuation to the above practices, few additional can be taken care
Thank you Arvind for your suggestions.
nice blog. Always be carefull when using "for all entries". Always check if the internal table is filled. Otherwise your performance is gone.
Nice Document, Thanks for sharing!
Thanks Hema
Nice blog...
thanks Arvind
Thanks for sharing !! 🙂
Thanks Akber
Thanks Harsha very useful document
Thanks Praveen
Hi,
a good and clear document.
Thanks for sharing.
Observation
about 5) point , please correct this: LOOP AT RESULT-PACAKGE
Cheers
Ivan
Thanks Ivan. Corrected.
Hi
Nice and Good document
Thanks,
Purushotham.
Thanks Purushotham. Can you rate it ?
Good Job.. Thanks for sharing.
🙂
Regards,
Krishna Chaitanya
Thanks Krishna. Can you rate it.
Hi Harshawardhan,
I think you got my rating.....right?
Regards,
Krishna Chaitanya.
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
Thanks Priyanka...!! 🙂
Good effort and explained clearly. Thanks for sharing
Thanks Priyanka..
Thanks for compiling the important points of the routines. It's a nice document and extremely helpful.
Regards,
Prasad
Thanks Prasad
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
Thanks Kamal. I will surely try to consider your suggetions.
Very useful... thanks! 🙂
Thanks a lot Rohini..!
Good one, Thanks for sharing....
Thank you Mithun..
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
Thanks Mayur for your words..!!
thanks for sharing nicely done work
Thank you Martin..
Useful one .Harsha Thanks for sharing
Thanks Ranbir..
Thanks Harsha for this helpful document.
Thanks Susheeth..
amazing Document..Its very easy to Understand..
Keep Blogging
Nice efforts to simply the things..
BR,
Prakash
Simple and yet useful information.
Thank you.