1- <!-- $PostgreSQL: pgsql/doc/src/sgml/plpgsql.sgml,v 1.130 2008/05/15 22:39:49 tgl Exp $ -->
1+ <!-- $PostgreSQL: pgsql/doc/src/sgml/plpgsql.sgml,v 1.131 2008/06/27 01:52:59 tgl Exp $ -->
22
33<chapter id="plpgsql">
44 <title><application>PL/pgSQL</application> - <acronym>SQL</acronym> Procedural Language</title>
@@ -1583,36 +1583,24 @@ SELECT * FROM getallfoo();
15831583 <para>
15841584 <command>IF</> and <command>CASE</> statements let you execute
15851585 alternative commands based on certain conditions.
1586- <application>PL/pgSQL</> hasfive forms of <command>IF</>:
1586+ <application>PL/pgSQL</> hasthree forms of <command>IF</>:
15871587 <itemizedlist>
15881588 <listitem>
15891589 <para><literal>IF ... THEN</></>
15901590 </listitem>
15911591 <listitem>
15921592 <para><literal>IF ... THEN ... ELSE</></>
15931593 </listitem>
1594- <listitem>
1595- <para><literal>IF ... THEN ... ELSE IF</></>
1596- </listitem>
15971594 <listitem>
15981595 <para><literal>IF ... THEN ... ELSIF ... THEN ... ELSE</></>
15991596 </listitem>
1600- <listitem>
1601- <para><literal>IF ... THEN ... ELSEIF ... THEN ... ELSE</></>
1602- </listitem>
16031597 </itemizedlist>
16041598
1605- andfour forms of <command>CASE</>:
1599+ andtwo forms of <command>CASE</>:
16061600 <itemizedlist>
1607- <listitem>
1608- <para><literal>CASE ... WHEN ... THEN ... END CASE</></>
1609- </listitem>
16101601 <listitem>
16111602 <para><literal>CASE ... WHEN ... THEN ... ELSE ... END CASE</></>
16121603 </listitem>
1613- <listitem>
1614- <para><literal>CASE WHEN ... THEN ... END CASE</></>
1615- </listitem>
16161604 <listitem>
16171605 <para><literal>CASE WHEN ... THEN ... ELSE ... END CASE</></>
16181606 </listitem>
@@ -1661,7 +1649,8 @@ END IF;
16611649 <literal>IF-THEN-ELSE</literal> statements add to
16621650 <literal>IF-THEN</literal> by letting you specify an
16631651 alternative set of statements that should be executed if the
1664- condition evaluates to false.
1652+ condition is not true. (Note this includes the case where the
1653+ condition evaluates to NULL.)
16651654 </para>
16661655
16671656 <para>
@@ -1687,37 +1676,7 @@ END IF;
16871676 </sect3>
16881677
16891678 <sect3>
1690- <title><literal>IF-THEN-ELSE IF</></title>
1691-
1692- <para>
1693- <literal>IF</literal> statements can be nested, as in the
1694- following example:
1695-
1696- <programlisting>
1697- IF demo_row.sex = 'm' THEN
1698- pretty_sex := 'man';
1699- ELSE
1700- IF demo_row.sex = 'f' THEN
1701- pretty_sex := 'woman';
1702- END IF;
1703- END IF;
1704- </programlisting>
1705- </para>
1706-
1707- <para>
1708- When you use this form, you are actually nesting an
1709- <literal>IF</literal> statement inside the
1710- <literal>ELSE</literal> part of an outer <literal>IF</literal>
1711- statement. Thus you need one <literal>END IF</literal>
1712- statement for each nested <literal>IF</literal> and one for the parent
1713- <literal>IF-ELSE</literal>. This is workable but grows
1714- tedious when there are many alternatives to be checked.
1715- Hence the next form.
1716- </para>
1717- </sect3>
1718-
1719- <sect3>
1720- <title><literal>IF-THEN-ELSIF-ELSE</></title>
1679+ <title><literal>IF-THEN-ELSIF</></title>
17211680
17221681<synopsis>
17231682IF <replaceable>boolean-expression</replaceable> THEN
@@ -1735,11 +1694,16 @@ END IF;
17351694</synopsis>
17361695
17371696 <para>
1738- <literal>IF-THEN-ELSIF-ELSE</> provides a more convenient
1739- method of checking many alternatives in one statement.
1740- Functionally it is equivalent to nested
1741- <literal>IF-THEN-ELSE-IF-THEN</> commands, but only one
1742- <literal>END IF</> is needed.
1697+ Sometimes there are more than just two alternatives.
1698+ <literal>IF-THEN-ELSIF</> provides a convenient
1699+ method of checking several alternatives in turn.
1700+ The <literal>IF</> conditions are tested successively
1701+ until the first one that is true is found. Then the
1702+ associated statement(s) are executed, after which control
1703+ passes to the next statement after <literal>END IF</>.
1704+ (Any subsequent <literal>IF</> conditions are <emphasis>not</>
1705+ tested.) If none of the <literal>IF</> conditions is true,
1706+ then the <literal>ELSE</> block (if any) is executed.
17431707 </para>
17441708
17451709 <para>
@@ -1758,14 +1722,33 @@ ELSE
17581722END IF;
17591723</programlisting>
17601724 </para>
1761- </sect3>
17621725
1763- <sect3>
1764- <title><literal>IF-THEN-ELSEIF-ELSE</></title>
1726+ <para>
1727+ The key word <literal>ELSIF</> can also be spelled
1728+ <literal>ELSEIF</>.
1729+ </para>
17651730
1766- <para>
1767- <literal>ELSEIF</> is an alias for <literal>ELSIF</>.
1768- </para>
1731+ <para>
1732+ An alternative way of accomplishing the same task is to nest
1733+ <literal>IF-THEN-ELSE</literal> statements, as in the
1734+ following example:
1735+
1736+ <programlisting>
1737+ IF demo_row.sex = 'm' THEN
1738+ pretty_sex := 'man';
1739+ ELSE
1740+ IF demo_row.sex = 'f' THEN
1741+ pretty_sex := 'woman';
1742+ END IF;
1743+ END IF;
1744+ </programlisting>
1745+ </para>
1746+
1747+ <para>
1748+ However, this method requires writing a matching <literal>END IF</>
1749+ for each <literal>IF</>, so it is much more cumbersome than
1750+ using <literal>ELSIF</> when there are many alternatives.
1751+ </para>
17691752 </sect3>
17701753
17711754 <sect3>
@@ -1853,6 +1836,13 @@ END CASE;
18531836</programlisting>
18541837 </para>
18551838
1839+ <para>
1840+ This form of <command>CASE</> is entirely equivalent to
1841+ <literal>IF-THEN-ELSIF</>, except for the rule that reaching
1842+ an omitted <literal>ELSE</> clause results in an error rather
1843+ than doing nothing.
1844+ </para>
1845+
18561846 </sect3>
18571847 </sect2>
18581848