Home » SQL & PL/SQL » SQL & PL/SQL » How to exit from a For Loop on a cursor correctly (Oracle 11.i)
How to exit from a For Loop on a cursor correctly [message #517375] Mon, 25 July 2011 11:08 Go to next message
ncsthbell
Messages: 10
Registered: September 2010
Location: North Carolina
Junior Member
I would like to exit from a cursor loop based on certain conditional checking. I am checking for a lot of different parameters and if they fail, I want to bypass it and fetch the next record in the cursor. I tried just putting an 'Exit' statement in the logic, but it fails. An example of my code is below:
For Row1 in cursor1
  Loop
    If amount < 0 then
       balance := 0;
    Else
       -- need to get the next cursor row
    end if
    If tx_credit < 0
       credit = 0;
    Else
       -- need to get the next cursor row
    End if
   --Did not include all of my 'if' statements in this sample
   End Loop;
Re: How to exit from a For Loop on a cursor correctly [message #517380 is a reply to message #517375] Mon, 25 July 2011 11:21 Go to previous messageGo to next message
cookiemonster
Messages: 13967
Registered: September 2008
Location: Rainy Manchester
Senior Member
What do you mean exit failed? Failed how?
It works fine for me.

Generally though the correct way is to change the where clause on the cursor so that it only returns the rows you actually want.
Re: How to exit from a For Loop on a cursor correctly [message #517389 is a reply to message #517375] Mon, 25 July 2011 11:56 Go to previous messageGo to next message
Michel Cadot
Messages: 68765
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
Where do amount and tx_credit come from ? The cursor?

Quote:
and fetch the next record in the cursor. I tried just putting an 'Exit' statement in the logic

And EXIT does not go to the next row, it leaves (goes outside) the loop.

Regards
Michel
Re: How to exit from a For Loop on a cursor correctly [message #517391 is a reply to message #517389] Mon, 25 July 2011 12:11 Go to previous messageGo to next message
ncsthbell
Messages: 10
Registered: September 2010
Location: North Carolina
Junior Member
My problem is that the 'Exit' is taking me out of the cursor loop, I want to get the next record in the cursor.

I have put the conditions on the where clause for the cursor as best I can. However, there is some very complicated conditional checking that is not easily done in the 'where' clause, therefore I am coding it as conditional checks.

The sample I put in this post is the basic sample of what I am trying to do, I did not include all of the conditional 'If, else, then' code. The column names I mentioned would be obtained through the cursor.
Re: How to exit from a For Loop on a cursor correctly [message #517392 is a reply to message #517391] Mon, 25 July 2011 12:15 Go to previous messageGo to next message
Michel Cadot
Messages: 68765
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
Quote:
have put the conditions on the where clause for the cursor as best I can. However, there is some very complicated conditional checking that is not easily done in the 'where' clause, therefore I am coding it as conditional checks.

If you post the complete problem maybe we can get a better solution than you did.

Quote:
My problem is that the 'Exit' is taking me out of the cursor loop,

This is what I said.
PL/SQL User's Guide and Reference
Chapter 4 Using PL/SQL Control Structures
Section Controlling Loop Iterations: LOOP and EXIT Statements

Regards
Michel

Re: How to exit from a For Loop on a cursor correctly [message #517393 is a reply to message #517391] Mon, 25 July 2011 12:15 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>I want to get the next record in the cursor.
Above occurs automatically after hitting END LOOP.
If you properly nest IF statements you can fall to the bottom of the LOOP as required.

As presented LOOP is of infinite variety
Re: How to exit from a For Loop on a cursor correctly [message #517395 is a reply to message #517393] Mon, 25 July 2011 12:41 Go to previous messageGo to next message
ncsthbell
Messages: 10
Registered: September 2010
Location: North Carolina
Junior Member
Was trying to avoid having to 'nest' all the "IF" statements as it gets so sloppy and hard to follow. However, if that is the only way I can get out and get the next cursor row, I guess I'll have to do it that way!.
Thanks
Re: How to exit from a For Loop on a cursor correctly [message #517397 is a reply to message #517395] Mon, 25 July 2011 12:44 Go to previous messageGo to next message
Michel Cadot
Messages: 68765
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
Michel Cadot wrote on Mon, 25 July 2011 19:15
...
If you post the complete problem maybe we can get a better solution than you did.
...

Re: How to exit from a For Loop on a cursor correctly [message #517407 is a reply to message #517392] Mon, 25 July 2011 14:42 Go to previous message
Solomon Yakobson
Messages: 3305
Registered: January 2010
Location: Connecticut, USA
Senior Member
Michel Cadot wrote on Mon, 25 July 2011 13:15
This is what I said.


We need to find out Oracle version first. If it is 11g, then OP could use CONTINUE (http://download.oracle.com/docs/cd/B28359_01/appdev.111/b28370/controlstructures.htm#i8296)

SY.
Previous Topic: Drop Table with special character
Next Topic: Replacing a Foreign key with another
Goto Forum:
  


Current Time: Sun Aug 10 10:10:48 CDT 2025