Re: Return values ???
Posted in 1999
Steve Barnes <SBarnes@IFMG.com> wrote: > I am curious as to why a FOREACH is considered better than OPEN, > WHILE, FETCH, CLOSE. I always thought that for large numbers of reads, > the overhead of a FOREACH instead of the alternative had performance > impacts. This was the thought a few years ago, and maybe I am behind > the times! > > Can you please clarify this for me please. I'm not sure. Which overheads? There are no overheads that I know of. A FOREACH loop is a shorthand for the loop originally shown, except that FOREACH correctly avoids fetching any data if the cursor fails to open. It is for that sort of reason that the FOREACH loop is better. Jonathan Leffler <jleffler@earthlink.net> wrote: > Octav Chiriac wrote: > > [...various variations on PREPARE and DECLARE...] > > OPEN qCursorName USING var1, var2 > > > > WHILE TRUE > > FETCH qCursorName INTO result1, result2 > > IF sqlca.sqlcode == NOTFOUND THEN > > EXIT WHILE > > END IF > > > > -- DO SOMETHING WITH YOUR results > > > > END WHILE > > > > CLOSE qCursorName > > > > FREE ... stuff > > It is much better to use a FOREACH loop because it is more > reliable: > > PREPARE p_stmt FROM "EXECUTE PROCEDURE theprocedure(?,?)" > DECLARE c_stmt FROM p_stmt > > FOREACH c_stmt USING var1, var2 INTO result1, result2 > ...Do Something With Your Results... > END FOREACH > > FREE c_stmt > FREE p_stmt -- Jonathan Leffler (jleffler@informix.com, jleffler@earthlink.net) Guardian of DBD::Informix v0.60 -- see http://www.perl.com/CPAN #include <disclaimer.h>