Subject Re: [ib-support] Breaking out of For Select...Do
Author Marco Bommeljé
Hi Joe,

In stead of "breaking out" the FOR SELECT, you can include the
terminating condition in the FOR SELECT itself:

the_total = 0; /* intialization required */
for select MY_NUM from MY_TABLE
where :the_total >= 500 /* <== where condition */
into :THIS_NUM
do
begin
...
end

Good luck,
Marco


Lucas Franzen wrote:
>
> Joe,
>
>
> > The table has the following values for THIS_NUM:
> > 100
> > 300
> > 200
> > 800
> > 150
> >
> > I want to loop through these rows.
> >
> > 1st iteration: MY_NUM = 100, THE_TOTAL = 100
> > 2nd iteration: MY_NUM = 300, THE_TOTAL = 400
> > 3rd iteration: MY_NUM = 200, THE_TOTAL = 600
> >
> > At this point, THE_TOTAL is over 500, so I'm done. I don't even want to
> > loop through the 4th and 5th rows.
>
> If you want to end the loop AND the procedure you can use EXIT:
>
>
> for select MY_NUM from MY_TABLE into :THIS_NUM
> do
> BEGIN
> THE_TOTAL = THE_TOTAL + THIS_NUM;
> if (THE_TOTAL >= 500) then
> begin
> SUSPEND; /* if needed */
> EXIT;
> end
> END
>
>
> If you want to continue with your procedure after the FOR..SELECT loop
> than the answer is: you can't. There is no BREAK statement.
>
> What you can do in that case is:
>
> declare variable sum_ok CHAR(1);
>
> sum_ok ='0';
> for select MY_NUM from MY_TABLE into :THIS_NUM
> do
> BEGIN
> if ( sum_ok = '0' ) then THE_TOTAL = THE_TOTAL + THIS_NUM;
> if (THE_TOTAL >= 500) then
> begin
> sum_ok = '1';
> end
> END
>
> this will prevent from further adding, BUT the 4th, 5th...nth record
> will be retrieved.
>
> So if you need it as a real break-statement it should be the easiest way
> to:
> - write a procedure that does use EXIT (as shown above)
> - call this procedure from your procedure.
>
> HTH
>
> Luc.
>
> *Yahoo! Groups Sponsor*
>
> What's new?
> <http://rd.yahoo.com/M=252745.3265183.4563152.1261774/D=egroupweb/S=1705115386:HM/A=1568509/R=1/*http://search.yahoo.com/tour>
>
>
> <http://rd.yahoo.com/M=252745.3265183.4563152.1261774/D=egroupweb/S=1705115386:HM/A=1568509/R=2/*http://search.yahoo.com?fr=ad-sp-lrec-ml-practice-ns>
>
>
> To unsubscribe from this group, send an email to:
> ib-support-unsubscribe@egroups.com
>
>
>
> Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service
> <http://docs.yahoo.com/info/terms/>.

--
-------------------------------------
-- Marco Bommeljé
-- Bommeljé Crompvoets en partners bv
-- W: www.bcp-software.nl
-- E: mbommelj@...
-- T: +31 (0)30 2428369
-------------------------------------