RE: Return values ???
Posted in 1999
This message is in MIME format. Since your mail reader does not understand this format, some or all of this message may not be legible. ------_=_NextPart_001_01BEE321.29EE0726 Content-Type: text/plain; charset="iso-8859-1" Jonathan, 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. Thanks Steve -----Original Message----- From: Jonathan Leffler [mailto:jleffler@earthlink.net] Sent: Monday, August 09, 1999 11:59 PM To: informix-list@iiug.org Subject: Re: Return values ??? 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 Don't forget to release both the statement and the cursor. And do use FOREACH rather than ever writing out the loop from spare parts (OPEN, WHILE, FETCH, CLOSE). This has been available since at latest the 4.14/6.02 release pair, and possibly as early as the 4.12/6.00 release pair. -- Jonathan Leffler (jleffler@informix.com, jleffler@earthlink.net) Guardian of DBD::Informix v0.60 -- see http://www.perl.com/CPAN #include <disclaimer.h> ------_=_NextPart_001_01BEE321.29EE0726 Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"> <HTML> <HEAD> <META HTTP-EQUIV=3D"Content-Type" CONTENT=3D"text/html; = charset=3Diso-8859-1"> <META NAME=3D"Generator" CONTENT=3D"MS Exchange Server version = 5.5.2448.0"> <TITLE>RE: Return values ???</TITLE> </HEAD> <BODY> <P><FONT SIZE=3D2 FACE=3D"Arial">Jonathan,</FONT> </P> <P><FONT SIZE=3D2 FACE=3D"Arial">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!</FONT></P> <P><FONT SIZE=3D2 FACE=3D"Arial">Can you please clarify this for me = please.</FONT> </P> <P><FONT SIZE=3D2 FACE=3D"Arial">Thanks</FONT> </P> <P><FONT SIZE=3D2 FACE=3D"Arial">Steve</FONT> </P> <UL><UL> <P><A NAME=3D"_MailData"><FONT SIZE=3D2 FACE=3D"Arial">-----Original = Message-----</FONT></A> <BR><B><FONT SIZE=3D2 FACE=3D"Arial">From: Jonathan Leffler = [<A = HREF=3D"mailto:jleffler@earthlink.net">mailto:jleffler@earthlink.net</A>= ]</FONT></B> <BR><B><FONT SIZE=3D2 FACE=3D"Arial">Sent: </FONT></B> <FONT = SIZE=3D2 FACE=3D"Arial">Monday, August 09, 1999 11:59 PM</FONT> <BR><B><FONT SIZE=3D2 = FACE=3D"Arial">To: </FONT></B> <FONT SIZE=3D2 = FACE=3D"Arial">informix-list@iiug.org</FONT> <BR><B><FONT SIZE=3D2 = FACE=3D"Arial">Subject: </FONT>= </B> <FONT SIZE=3D2 FACE=3D"Arial">Re: Return values ???</FONT> </P> <P><FONT SIZE=3D2 FACE=3D"Arial">Octav Chiriac wrote:</FONT> <BR><FONT SIZE=3D2 FACE=3D"Arial">> [...various variations on = PREPARE and DECLARE...]</FONT> <BR><FONT SIZE=3D2 FACE=3D"Arial">> OPEN qCursorName USING var1, = var2</FONT> <BR><FONT SIZE=3D2 FACE=3D"Arial">></FONT> <BR><FONT SIZE=3D2 FACE=3D"Arial">> WHILE TRUE</FONT> <BR><FONT SIZE=3D2 FACE=3D"Arial">> FETCH qCursorName INTO = result1, result2</FONT> <BR><FONT SIZE=3D2 FACE=3D"Arial">> IF sqlca.sqlcode =3D=3D = NOTFOUND THEN</FONT> <BR><FONT SIZE=3D2 FACE=3D"Arial">> EXIT = WHILE</FONT> <BR><FONT SIZE=3D2 FACE=3D"Arial">> END IF</FONT> <BR><FONT SIZE=3D2 FACE=3D"Arial">></FONT> <BR><FONT SIZE=3D2 FACE=3D"Arial">> -- DO SOMETHING WITH YOUR = results</FONT> <BR><FONT SIZE=3D2 FACE=3D"Arial">></FONT> <BR><FONT SIZE=3D2 FACE=3D"Arial">> END WHILE</FONT> <BR><FONT SIZE=3D2 FACE=3D"Arial">></FONT> <BR><FONT SIZE=3D2 FACE=3D"Arial">> CLOSE qCursorName</FONT> <BR><FONT SIZE=3D2 FACE=3D"Arial">></FONT> <BR><FONT SIZE=3D2 FACE=3D"Arial">> FREE ... stuff</FONT> </P> <P><FONT SIZE=3D2 FACE=3D"Arial">It is much better to use a FOREACH = loop because it is more reliable:</FONT> </P> <P><FONT SIZE=3D2 FACE=3D"Arial">PREPARE p_stmt FROM "EXECUTE = PROCEDURE theprocedure(?,?)"</FONT> <BR><FONT SIZE=3D2 FACE=3D"Arial">DECLARE c_stmt FROM p_stmt</FONT> </P> <P><FONT SIZE=3D2 FACE=3D"Arial">FOREACH c_stmt USING var1, var2 INTO = result1, result2</FONT> <BR> <FONT SIZE=3D2 = FACE=3D"Arial">...Do Something With Your Results...</FONT> <BR><FONT SIZE=3D2 FACE=3D"Arial">END FOREACH</FONT> </P> <P><FONT SIZE=3D2 FACE=3D"Arial">FREE c_stmt</FONT> <BR><FONT SIZE=3D2 FACE=3D"Arial">FREE p_stmt</FONT> </P> <P><FONT SIZE=3D2 FACE=3D"Arial">Don't forget to release both the = statement and the cursor. And</FONT> <BR><FONT SIZE=3D2 FACE=3D"Arial">do use FOREACH rather than ever = writing out the loop from spare</FONT> <BR><FONT SIZE=3D2 FACE=3D"Arial">parts (OPEN, WHILE, FETCH, = CLOSE).</FONT> </P> <P><FONT SIZE=3D2 FACE=3D"Arial">This has been available since at = latest the 4.14/6.02 release pair,</FONT> <BR><FONT SIZE=3D2 FACE=3D"Arial">and possibly as early as the = 4.12/6.00 release pair.</FONT> </P> <P><FONT SIZE=3D2 FACE=3D"Arial">-- </FONT> <BR><FONT SIZE=3D2 FACE=3D"Arial">Jonathan Leffler = (jleffler@informix.com, jleffler@earthlink.net)</FONT> <BR><FONT SIZE=3D2 FACE=3D"Arial">Guardian of DBD::Informix v0.60 -- = see <A HREF=3D"http://www.perl.com/CPAN" = TARGET=3D"_blank">http://www.perl.com/CPAN</A></FONT> <BR><FONT SIZE=3D2 FACE=3D"Arial">#include <disclaimer.h></FONT> </P> </UL></UL>@