mirror of
https://github.com/postgres/postgres.git
synced 2025-07-30 11:03:19 +03:00
Support SQL/PSM-compatible CASE statement in plpgsql.
Pavel Stehule
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
<!-- $PostgreSQL: pgsql/doc/src/sgml/errcodes.sgml,v 1.23 2008/01/15 01:36:53 tgl Exp $ -->
|
||||
<!-- $PostgreSQL: pgsql/doc/src/sgml/errcodes.sgml,v 1.24 2008/05/15 22:39:48 tgl Exp $ -->
|
||||
|
||||
<appendix id="errcodes-appendix">
|
||||
<title><productname>PostgreSQL</productname> Error Codes</title>
|
||||
@ -62,14 +62,14 @@
|
||||
|
||||
<tgroup cols="3">
|
||||
<colspec colnum="1" colname="errorcode">
|
||||
<colspec colnum="3" colname="constant">
|
||||
<spanspec namest="errorcode" nameend="constant" spanname="span13">
|
||||
<colspec colnum="3" colname="condname">
|
||||
<spanspec namest="errorcode" nameend="condname" spanname="span13">
|
||||
|
||||
<thead>
|
||||
<row>
|
||||
<entry>Error Code</entry>
|
||||
<entry>Meaning</entry>
|
||||
<entry>Constant</entry>
|
||||
<entry>Condition Name</entry>
|
||||
</row>
|
||||
</thead>
|
||||
|
||||
@ -292,6 +292,17 @@
|
||||
</row>
|
||||
|
||||
|
||||
<row>
|
||||
<entry spanname="span13"><emphasis role="bold">Class 20 — Case Not Found</></entry>
|
||||
</row>
|
||||
|
||||
<row>
|
||||
<entry><literal>20000</literal></entry>
|
||||
<entry>CASE NOT FOUND</entry>
|
||||
<entry>case_not_found</entry>
|
||||
</row>
|
||||
|
||||
|
||||
<row>
|
||||
<entry spanname="span13"><emphasis role="bold">Class 21 — Cardinality Violation</></entry>
|
||||
</row>
|
||||
|
@ -1,4 +1,4 @@
|
||||
<!-- $PostgreSQL: pgsql/doc/src/sgml/plpgsql.sgml,v 1.129 2008/05/13 22:10:29 tgl Exp $ -->
|
||||
<!-- $PostgreSQL: pgsql/doc/src/sgml/plpgsql.sgml,v 1.130 2008/05/15 22:39:49 tgl Exp $ -->
|
||||
|
||||
<chapter id="plpgsql">
|
||||
<title><application>PL/pgSQL</application> - <acronym>SQL</acronym> Procedural Language</title>
|
||||
@ -1581,9 +1581,9 @@ SELECT * FROM getallfoo();
|
||||
<title>Conditionals</title>
|
||||
|
||||
<para>
|
||||
<literal>IF</> statements let you execute commands based on
|
||||
certain conditions. <application>PL/pgSQL</> has five forms of
|
||||
<literal>IF</>:
|
||||
<command>IF</> and <command>CASE</> statements let you execute
|
||||
alternative commands based on certain conditions.
|
||||
<application>PL/pgSQL</> has five forms of <command>IF</>:
|
||||
<itemizedlist>
|
||||
<listitem>
|
||||
<para><literal>IF ... THEN</></>
|
||||
@ -1601,6 +1601,22 @@ SELECT * FROM getallfoo();
|
||||
<para><literal>IF ... THEN ... ELSEIF ... THEN ... ELSE</></>
|
||||
</listitem>
|
||||
</itemizedlist>
|
||||
|
||||
and four forms of <command>CASE</>:
|
||||
<itemizedlist>
|
||||
<listitem>
|
||||
<para><literal>CASE ... WHEN ... THEN ... END CASE</></>
|
||||
</listitem>
|
||||
<listitem>
|
||||
<para><literal>CASE ... WHEN ... THEN ... ELSE ... END CASE</></>
|
||||
</listitem>
|
||||
<listitem>
|
||||
<para><literal>CASE WHEN ... THEN ... END CASE</></>
|
||||
</listitem>
|
||||
<listitem>
|
||||
<para><literal>CASE WHEN ... THEN ... ELSE ... END CASE</></>
|
||||
</listitem>
|
||||
</itemizedlist>
|
||||
</para>
|
||||
|
||||
<sect3>
|
||||
@ -1751,6 +1767,93 @@ END IF;
|
||||
<literal>ELSEIF</> is an alias for <literal>ELSIF</>.
|
||||
</para>
|
||||
</sect3>
|
||||
|
||||
<sect3>
|
||||
<title>Simple <literal>CASE</></title>
|
||||
|
||||
<synopsis>
|
||||
CASE <replaceable>search-expression</replaceable>
|
||||
WHEN <replaceable>expression</replaceable> <optional>, <replaceable>expression</replaceable> <optional> ... </optional></optional> THEN
|
||||
<replaceable>statements</replaceable>
|
||||
<optional> WHEN <replaceable>expression</replaceable> <optional>, <replaceable>expression</replaceable> <optional> ... </optional></optional> THEN
|
||||
<replaceable>statements</replaceable>
|
||||
... </optional>
|
||||
<optional> ELSE
|
||||
<replaceable>statements</replaceable> </optional>
|
||||
END CASE;
|
||||
</synopsis>
|
||||
|
||||
<para>
|
||||
The simple form of <command>CASE</> provides conditional execution
|
||||
based on equality of operands. The <replaceable>search-expression</>
|
||||
is evaluated (once) and successively compared to each
|
||||
<replaceable>expression</> in the <literal>WHEN</> clauses.
|
||||
If a match is found, then the corresponding
|
||||
<replaceable>statements</replaceable> are executed, and then control
|
||||
passes to the next statement after <literal>END CASE</>. (Subsequent
|
||||
<literal>WHEN</> expressions are not evaluated.) If no match is
|
||||
found, the <literal>ELSE</> <replaceable>statements</replaceable> are
|
||||
executed; but if <literal>ELSE</> is not present, then a
|
||||
<literal>CASE_NOT_FOUND</literal> exception is raised.
|
||||
</para>
|
||||
|
||||
<para>
|
||||
Here is a simple example:
|
||||
|
||||
<programlisting>
|
||||
CASE x
|
||||
WHEN 1, 2 THEN
|
||||
msg := 'one or two';
|
||||
ELSE
|
||||
msg := 'other value than one or two';
|
||||
END CASE;
|
||||
</programlisting>
|
||||
</para>
|
||||
</sect3>
|
||||
|
||||
<sect3>
|
||||
<title>Searched <literal>CASE</></title>
|
||||
|
||||
<synopsis>
|
||||
CASE
|
||||
WHEN <replaceable>boolean-expression</replaceable> THEN
|
||||
<replaceable>statements</replaceable>
|
||||
<optional> WHEN <replaceable>boolean-expression</replaceable> THEN
|
||||
<replaceable>statements</replaceable>
|
||||
... </optional>
|
||||
<optional> ELSE
|
||||
<replaceable>statements</replaceable> </optional>
|
||||
END CASE;
|
||||
</synopsis>
|
||||
|
||||
<para>
|
||||
The searched form of <command>CASE</> provides conditional execution
|
||||
based on truth of boolean expressions. Each <literal>WHEN</> clause's
|
||||
<replaceable>boolean-expression</replaceable> is evaluated in turn,
|
||||
until one is found that yields <literal>true</>. Then the
|
||||
corresponding <replaceable>statements</replaceable> are executed, and
|
||||
then control passes to the next statement after <literal>END CASE</>.
|
||||
(Subsequent <literal>WHEN</> expressions are not evaluated.)
|
||||
If no true result is found, the <literal>ELSE</>
|
||||
<replaceable>statements</replaceable> are executed;
|
||||
but if <literal>ELSE</> is not present, then a
|
||||
<literal>CASE_NOT_FOUND</literal> exception is raised.
|
||||
</para>
|
||||
|
||||
<para>
|
||||
Here is an example:
|
||||
|
||||
<programlisting>
|
||||
CASE
|
||||
WHEN x BETWEEN 0 AND 10 THEN
|
||||
msg := 'value is between zero and ten';
|
||||
WHEN x BETWEEN 11 AND 20 THEN
|
||||
msg := 'value is between eleven and twenty';
|
||||
END CASE;
|
||||
</programlisting>
|
||||
</para>
|
||||
|
||||
</sect3>
|
||||
</sect2>
|
||||
|
||||
<sect2 id="plpgsql-control-structures-loops">
|
||||
|
Reference in New Issue
Block a user