Saturday, March 3, 2012

Endless loops in ABAP

I haven't updated my blog for quite long time. This is mostly because I’m currently involved in project which requires me to travel a lot. Therefore today here comes again only very short tip. To be more precise the tip will be how to code so called endless loops in ABAP. 

First let me explain what it is endless loop. It is a part of code which is being constantly executed over and over again until certain condition (there can be terminating condition or condition that is never met) is not fulfilled or loop is terminated by user. In other words we call the endless loop as infinite loop or unproductive loop.

Secondly let me explain why we (from time to time) need that. There might be situation that you need to check or monitor behavior of system while runtime of your program. Someone might say that for this I could set a breakpoint and achieve the same. Yes that would be possible but imagine you run your program as a job and within distributed SAP environment comprised from several application servers. In such a scenario you cannot be sure by which server your program will be executed. Therefore you let your program run in endless look.  Afterwards you observe on which server it runs and you switch into its runtime from e.g. TA SM50 via Debugging functionality (process, menu Program-> Debug program).  Actually in this case loop gives you a time to prepare TA50 and get into the processing of program as well.

In case of BW there are certain situations where infinite loops are handy as well. As all BW processing usually takes place on background (e.g. extraction: DTP, InfoPackages, Process Chains or BEx reporting variables programming) endless loops are needed here as well. I’m pretty sure you can figure out our examples.

Here’s one example of the endless loop:

data: debug(1).
do.
if debug = 'X'.
exit.
endif.
enddo.

If you are done with program’s environment monitoring you can easily escape this loop by manipulating with value of variable debug setting it to ’X’.

See blog on similar ABAP topic: If 1 equals 2, what’s the purpose?

No comments: