This website is not affiliated with, sponsored by, or approved by SAP AG.

How to EXIT in a PERFORM.

Development (ABAP Development WorkBench, ABAP/4 programming)

Moderators: thx4allthefish, Snowy, Gothmog, YuriT

How to EXIT in a PERFORM.

Postby Jkeri » Thu Mar 22, 2012 2:32 pm

I do a PERFORM to a routine. In that routine if a certain condtion exists, I want to exit out of the routine and go to the ENDFORM. Can this be done in ABAP?


EXAMPLE code.

Perform 1000_read.


form 1000_read.
select ..... from database table
if sy-dbcnt = 0.
stop doing any more code and drop out of the FORM.
endif.

append.....
delete .......

ENDFORM.

Jkeri
 
Posts: 59
Joined: Thu Jul 21, 2011 12:44 pm

Re: How to EXIT in a PERFORM.

Postby Sharpshooter » Thu Mar 22, 2012 2:57 pm

You answered your own question in the subject of your post!
Good luck!
Sharpshooter
 
Posts: 955
Joined: Wed Mar 17, 2010 12:01 pm
Location: In the dark

Re: How to EXIT in a PERFORM.

Postby Jkeri » Thu Mar 22, 2012 3:01 pm

ok,, so you are saying that by using 'EXIT' i can drop to the ENDFORM?
Jkeri
 
Posts: 59
Joined: Thu Jul 21, 2011 12:44 pm

Re: How to EXIT in a PERFORM.

Postby YuriT » Fri Mar 23, 2012 1:13 am

Jkeri wrote:ok,, so you are saying that by using 'EXIT' i can drop to the ENDFORM?


You bet!
YuriT
 
Posts: 890
Joined: Fri Feb 03, 2006 6:40 am
Location: Basel/Riga

Re: How to EXIT in a PERFORM.

Postby Rich » Fri Mar 23, 2012 2:35 am

Or use the else clause in the if statement, or re-write the if statement:

Multiple exit points from a function module or procedure is generally not good practice. There should be one entry point and one exit point for each routine, so:

Code: Select all
Perform 1000_read.


form 1000_read.
     select ..... from database table
     if sy-dbcnt <> 0.
        append.....
        delete .......
     endif.
ENDFORM.
Regards

Rich

Image
Abap KC
SFMDR
Rich
 
Posts: 6926
Joined: Thu Oct 31, 2002 4:47 pm
Location: Geneva

Re: How to EXIT in a PERFORM.

Postby DeathAndPain » Fri Mar 30, 2012 5:47 am

EXIT will only leave the form when there is no other command structure (like a LOOP) open. In his example he suggested that he might be in a SELECT-ENDSELECT-loop, in which case EXIT would only leave that one, but not drop out of the form (meaning that after the loop, he would have to add more if-conditions that lead to additional EXIT(s) in order to leave the form).

The true answer to his question is not the EXIT command, but the RETURN command. RETURN exits the current FORM-Block no matter what, also leaving any current loops. RETURN does exactly what he wants. EXIT is equivalent only if he is not within a loop.
Image
DeathAndPain
 
Posts: 79
Joined: Wed Oct 06, 2004 12:49 am

Re: How to EXIT in a PERFORM.

Postby Sharpshooter » Fri Mar 30, 2012 6:45 am

D&P, you are correct, of course.
But I would add that if one adheres to quality programming standards, Select...Endselect should be a rarity.
Good luck!
Sharpshooter
 
Posts: 955
Joined: Wed Mar 17, 2010 12:01 pm
Location: In the dark

Re: How to EXIT in a PERFORM.

Postby DeathAndPain » Fri Mar 30, 2012 7:54 am

I disagree on that. SELECT... ENDSELECT is often a good way to program very well readable code. I remember the times of 3.1x-releases when SELECT...ENDSELECT was horribly slow, so that it was a good idea to do a SELECT INTO TABLE with a subsequent LOOP simply for performance reasons. I find that this is no longer necessary though, and SELECT...ENDSELECT is better readable than a JOIN or a subsequent LOOP.

As far as this thread is concerned, EXIT will not exit the current form in a LOOP as well (and you certainly agree that LOOPs are necessary in many cases). RETURN is the failsafe alternative.
Image
DeathAndPain
 
Posts: 79
Joined: Wed Oct 06, 2004 12:49 am

Re: How to EXIT in a PERFORM.

Postby Sharpshooter » Fri Mar 30, 2012 9:06 am

We've certainly derailed this topic now!

My comment regarding select...endselect was an adjunct, not necessarily related to the EXIT issue.
I don't necessarily disagree with you. The best method to select from the database will indeed vary depending on factors such as volume of data and the level of subsequent processing required. There is a tradeoff between database performance and memory consumption.
Good luck!
Sharpshooter
 
Posts: 955
Joined: Wed Mar 17, 2010 12:01 pm
Location: In the dark

Re: How to EXIT in a PERFORM.

Postby Rich » Mon Apr 02, 2012 6:34 am

Sharpshooter wrote:We've certainly derailed this topic now!

My comment regarding select...endselect was an adjunct, not necessarily related to the EXIT issue.
I don't necessarily disagree with you. The best method to select from the database will indeed vary depending on factors such as volume of data and the level of subsequent processing required. There is a tradeoff between database performance and memory consumption.


For each iteration around a select-endselect loop you get two database accesses. For a select - into you only get two regardless of how many rows are returned. No contest in my book.
Regards

Rich

Image
Abap KC
SFMDR
Rich
 
Posts: 6926
Joined: Thu Oct 31, 2002 4:47 pm
Location: Geneva

Re: How to EXIT in a PERFORM.

Postby Sharpshooter » Mon Apr 02, 2012 7:41 am

Rich wrote:
Sharpshooter wrote:We've certainly derailed this topic now!

My comment regarding select...endselect was an adjunct, not necessarily related to the EXIT issue.
I don't necessarily disagree with you. The best method to select from the database will indeed vary depending on factors such as volume of data and the level of subsequent processing required. There is a tradeoff between database performance and memory consumption.


For each iteration around a select-endselect loop you get two database accesses. For a select - into you only get two regardless of how many rows are returned. No contest in my book.


No doubt - I was just in no mood to start a debate!
Good luck!
Sharpshooter
 
Posts: 955
Joined: Wed Mar 17, 2010 12:01 pm
Location: In the dark


Return to ABAP

Who is online

Users browsing this forum: No registered users and 17 guests




This website is not affiliated with, sponsored by, or approved by SAP AG.