1- <!-- $PostgreSQL: pgsql/doc/src/sgml/plpgsql.sgml,v 1.113 2007/07/14 23:02:25 tgl Exp $ -->
1+ <!-- $PostgreSQL: pgsql/doc/src/sgml/plpgsql.sgml,v 1.114 2007/07/15 00:45:16 tgl Exp $ -->
22
33<chapter id="plpgsql">
44 <title><application>PL/pgSQL</application> - <acronym>SQL</acronym> Procedural Language</title>
@@ -1757,7 +1757,7 @@ END LOOP;
17571757 </para>
17581758 </sect3>
17591759
1760- <sect3>
1760+ <sect3 id="plpgsql-integer-for" >
17611761 <title><literal>FOR</> (integer variant)</title>
17621762
17631763<synopsis>
@@ -1777,25 +1777,24 @@ END LOOP <optional> <replaceable>label</replaceable> </optional>;
17771777 the lower and upper bound of the range are evaluated once when entering
17781778 the loop. If the <literal>BY</> clause isn't specified the iteration
17791779 step is 1, otherwise it's the value specified in the <literal>BY</>
1780- clause. If <literal>REVERSE</> is specified then the step value is
1780+ clause, which again is evaluated once on loop entry.
1781+ If <literal>REVERSE</> is specified then the step value is
17811782 subtracted, rather than added, after each iteration.
17821783 </para>
17831784
17841785 <para>
17851786 Some examples of integer <literal>FOR</> loops:
17861787<programlisting>
17871788FOR i IN 1..10 LOOP
1788- -- some computations here
1789- RAISE NOTICE 'i is %', i;
1789+ -- i will take on the values 1,2,3,4,5,6,7,8,9,10 within the loop
17901790END LOOP;
17911791
17921792FOR i IN REVERSE 10..1 LOOP
1793- --some computations here
1793+ --i will take on the values 10,9,8,7,6,5,4,3,2,1 within the loop
17941794END LOOP;
17951795
17961796FOR i IN REVERSE 10..1 BY 2 LOOP
1797- -- some computations here
1798- RAISE NOTICE 'i is %', i;
1797+ -- i will take on the values 10,8,6,4,2 within the loop
17991798END LOOP;
18001799</programlisting>
18011800 </para>
@@ -1805,6 +1804,13 @@ END LOOP;
18051804 in the <literal>REVERSE</> case), the loop body is not
18061805 executed at all. No error is raised.
18071806 </para>
1807+
1808+ <para>
1809+ If a <replaceable>label</replaceable> is attached to the
1810+ <literal>FOR</> loop then the integer loop variable can be
1811+ referenced with a qualified name, using that
1812+ <replaceable>label</replaceable>.
1813+ </para>
18081814 </sect3>
18091815 </sect2>
18101816
@@ -3654,6 +3660,18 @@ a_output := a_output || $$ if v_$$ || referrer_keys.kind || $$ like '$$
36543660 </para>
36553661 </listitem>
36563662
3663+ <listitem>
3664+ <para>
3665+ Integer <command>FOR</> loops with <literal>REVERSE</> work
3666+ differently: <application>PL/SQL</> counts down from the second
3667+ number to the first, while <application>PL/pgSQL</> counts down
3668+ from the first number to the second, requiring the loop bounds
3669+ to be swapped when porting. This incompatibility is unfortunate
3670+ but is unlikely to be changed. (See <xref
3671+ linkend="plpgsql-integer-for">.)
3672+ </para>
3673+ </listitem>
3674+
36573675 </itemizedlist>
36583676 </para>
36593677