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 $ -->
2
2
3
3
<chapter id="plpgsql">
4
4
<title><application>PL/pgSQL</application> - <acronym>SQL</acronym> Procedural Language</title>
@@ -1583,36 +1583,24 @@ SELECT * FROM getallfoo();
1583
1583
<para>
1584
1584
<command>IF</> and <command>CASE</> statements let you execute
1585
1585
alternative commands based on certain conditions.
1586
- <application>PL/pgSQL</> has five forms of <command>IF</>:
1586
+ <application>PL/pgSQL</> has three forms of <command>IF</>:
1587
1587
<itemizedlist>
1588
1588
<listitem>
1589
1589
<para><literal>IF ... THEN</></>
1590
1590
</listitem>
1591
1591
<listitem>
1592
1592
<para><literal>IF ... THEN ... ELSE</></>
1593
1593
</listitem>
1594
- <listitem>
1595
- <para><literal>IF ... THEN ... ELSE IF</></>
1596
- </listitem>
1597
1594
<listitem>
1598
1595
<para><literal>IF ... THEN ... ELSIF ... THEN ... ELSE</></>
1599
1596
</listitem>
1600
- <listitem>
1601
- <para><literal>IF ... THEN ... ELSEIF ... THEN ... ELSE</></>
1602
- </listitem>
1603
1597
</itemizedlist>
1604
1598
1605
- and four forms of <command>CASE</>:
1599
+ and two forms of <command>CASE</>:
1606
1600
<itemizedlist>
1607
- <listitem>
1608
- <para><literal>CASE ... WHEN ... THEN ... END CASE</></>
1609
- </listitem>
1610
1601
<listitem>
1611
1602
<para><literal>CASE ... WHEN ... THEN ... ELSE ... END CASE</></>
1612
1603
</listitem>
1613
- <listitem>
1614
- <para><literal>CASE WHEN ... THEN ... END CASE</></>
1615
- </listitem>
1616
1604
<listitem>
1617
1605
<para><literal>CASE WHEN ... THEN ... ELSE ... END CASE</></>
1618
1606
</listitem>
@@ -1661,7 +1649,8 @@ END IF;
1661
1649
<literal>IF-THEN-ELSE</literal> statements add to
1662
1650
<literal>IF-THEN</literal> by letting you specify an
1663
1651
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.)
1665
1654
</para>
1666
1655
1667
1656
<para>
@@ -1687,37 +1676,7 @@ END IF;
1687
1676
</sect3>
1688
1677
1689
1678
<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>
1721
1680
1722
1681
<synopsis>
1723
1682
IF <replaceable>boolean-expression</replaceable> THEN
@@ -1735,11 +1694,16 @@ END IF;
1735
1694
</synopsis>
1736
1695
1737
1696
<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.
1743
1707
</para>
1744
1708
1745
1709
<para>
@@ -1758,14 +1722,33 @@ ELSE
1758
1722
END IF;
1759
1723
</programlisting>
1760
1724
</para>
1761
- </sect3>
1762
1725
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>
1765
1730
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>
1769
1752
</sect3>
1770
1753
1771
1754
<sect3>
@@ -1853,6 +1836,13 @@ END CASE;
1853
1836
</programlisting>
1854
1837
</para>
1855
1838
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
+
1856
1846
</sect3>
1857
1847
</sect2>
1858
1848
0 commit comments