Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.misc -> Re: what is the SQL equivalent of 'continue' and 'break' in C ?

Re: what is the SQL equivalent of 'continue' and 'break' in C ?

From: Ed Prochak <edprochak_at_gmail.com>
Date: 10 Aug 2006 12:40:43 -0700
Message-ID: <1155238843.193968.139790@m79g2000cwm.googlegroups.com>

happyardy_at_gmail.com wrote:
> DA Morgan wrote:
> > happyardy_at_gmail.com wrote:

>[]

> > >
> > > Daniel,
> > > I try to stay away from GOTO. GOTOs are not evils and are helpful
> > > sometimes but still I dont like to use them. I avoid them as much as
> > > possible.
> > > I was trying to find out if SQL has any keyword that is equivalent of
> > > 'continue' in C. C has GOTO too and I have always wanted to avoid that.
> > > thanks
> > > - Ardy
> >
> > In answer to your question ... no there isn't. But also in your
> > situation there is no functional difference between the two.
> >
> > Another option would be this though I don't like it much either:
> >
> > DECLARE
> > break EXCEPTION;
> > BEGIN
> > LOOP
> > BEGIN
> > <your code here>
> > RAISE break;
> > <the rest of your code here>
> > EXCEPTION
> > WHEN break THEN
> > NULL;
> > END;
> > END LOOP;
> > END;
> >
> > --
> > Daniel A. Morgan
> > University of Washington
> > damorgan_at_x.washington.edu
> > (replace x with u to respond)
> > Puget Sound Oracle Users Group
> > www.psoug.org
>
>

> Thanks, Daniel.

Daniel,
that is the corrrect way to do it in PL/SQL IMO. It uses the control structures available.
but the fact is there is a way to avoid the RAISE/EXCEPTION. the RAISE is controlled by an IF, so the condition of the IF just needs to be negated. IOW

instead of your example it could be:

the before
BEGIN
   LOOP

     BEGIN
       <your code here>
       IF ( condition)  THEN RAISE break;
       END IF;
       <the rest of your code here>> >      EXCEPTION
       WHEN break THEN
         NULL;
     END;

   END LOOP;
END; the after
BEGIN
   LOOP
     BEGIN
       <your code here>
       IF (NOT  (condition) )  THEN
       <the rest of your code here>
       END IF;

   END LOOP;
END; and a final comment about continue. In C continue is just shorthand for a GOTO <<end_of loop label>>
the C compiler does precisely the same operations and generates the same object code for both of these:

while (test)
{

   <<some code>>
  if ( condition ) continue ;
  << other code>>
}

while (test)
{

   <<some code>>
  if ( condition ) goto continuelabel;
  << other code>>
continuelabel:
}

So if you really want to write C programs in PL/SQL, then use the GOTO.

   Ed Received on Thu Aug 10 2006 - 14:40:43 CDT

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US