1
1
<!--
2
- $Header: /cvsroot/pgsql/doc/src/sgml/plpgsql.sgml,v 1.11 2002/11/15 03:22:30 momjian Exp $
2
+ $Header: /cvsroot/pgsql/doc/src/sgml/plpgsql.sgml,v 1.12 2002/11/23 03:59:05 momjian Exp $
3
3
-->
4
4
5
5
<chapter id="plpgsql">
@@ -674,24 +674,25 @@ RENAME this_var TO that_var;
674
674
<title>Expressions</title>
675
675
676
676
<para>
677
- All expressions used in <application>PL/pgSQL</application> statements
678
- are processed using the server's regular SQL executor. Expressions that
679
- appear to contain
680
- constants may in fact require run-time evaluation
681
- (e.g. <literal>'now'</literal> for the
682
- < type>timestamp</type> type ) so
683
- it is impossible for the <application>PL/pgSQL</application> parser
684
- to identify real constant values other than the NULL keyword. All
685
- expressions are evaluated internally by executing a query
677
+ All expressions used in <application>PL/pgSQL</application>
678
+ statements are processed using the server's regular
679
+ <acronym>SQL</acronym> executor. Expressions that appear to
680
+ contain constants may in fact require run-time evaluation
681
+ (e.g. <literal>'now'</literal> for the <type>timestamp</type>
682
+ type) so it is impossible for the
683
+ <application>PL/pgSQL</application> parser to identify real
684
+ constant values other than the NULL keyword. All expressions are
685
+ evaluated internally by executing a query
686
686
<synopsis>
687
687
SELECT <replaceable>expression</replaceable>
688
688
</synopsis>
689
- using the <acronym>SPI</acronym> manager. In the expression, occurrences
690
- of <application>PL/pgSQL</application> variable
689
+ using the <acronym>SPI</acronym> manager. In the expression,
690
+ occurrences of <application>PL/pgSQL</application> variable
691
691
identifiers are replaced by parameters and the actual values from
692
692
the variables are passed to the executor in the parameter array.
693
- This allows the query plan for the SELECT to be prepared just once
694
- and then re-used for subsequent evaluations.
693
+ This allows the query plan for the <command>SELECT</command> to
694
+ be prepared just once and then re-used for subsequent
695
+ evaluations.
695
696
</para>
696
697
697
698
<para>
@@ -1100,41 +1101,43 @@ GET DIAGNOSTICS <replaceable>variable</replaceable> = <replaceable>item</replace
1100
1101
<itemizedlist>
1101
1102
<listitem>
1102
1103
<para>
1103
- A SELECT INTO statement sets <literal>FOUND</literal>
1104
- true if it returns a row, false if no row is returned.
1104
+ A <command>SELECT INTO</command> statement sets
1105
+ <literal>FOUND</literal> true if it returns a row, false if no
1106
+ row is returned.
1105
1107
</para>
1106
1108
</listitem>
1107
1109
<listitem>
1108
1110
<para>
1109
- A PERFORM statement sets <literal>FOUND</literal>
1111
+ A <command> PERFORM</> statement sets <literal>FOUND</literal>
1110
1112
true if it produces (discards) a row, false if no row is
1111
1113
produced.
1112
1114
</para>
1113
1115
</listitem>
1114
1116
<listitem>
1115
1117
<para>
1116
- UPDATE, INSERT, and DELETE statements set
1117
- <literal>FOUND</literal> true if at least one row is
1118
- affected, false if no row is affected.
1118
+ <command> UPDATE</>, <command> INSERT</> , and <command> DELETE</>
1119
+ statements set <literal>FOUND</literal> true if at least one
1120
+ row is affected, false if no row is affected.
1119
1121
</para>
1120
1122
</listitem>
1121
1123
<listitem>
1122
1124
<para>
1123
- A FETCH statement sets <literal>FOUND</literal>
1125
+ A <command> FETCH</> statement sets <literal>FOUND</literal>
1124
1126
true if it returns a row, false if no row is returned.
1125
1127
</para>
1126
1128
</listitem>
1127
1129
<listitem>
1128
1130
<para>
1129
- A FOR statement sets <literal>FOUND</literal>
1130
- true if it iterates one or more times, else false.
1131
- This applies to all three variants of the FOR statement
1132
- (integer FOR loops, record-set FOR loops, and dynamic
1133
- record-set FOR loops). <literal>FOUND</literal> is only set
1134
- when the FOR loop exits: inside the execution of the loop,
1135
- <literal>FOUND</literal> is not modified by the FOR statement,
1136
- although it may be changed by the execution of other
1137
- statements within the loop body.
1131
+ A <command>FOR</> statement sets <literal>FOUND</literal> true
1132
+ if it iterates one or more times, else false. This applies to
1133
+ all three variants of the <command>FOR</> statement (integer
1134
+ <command>FOR</> loops, record-set <command>FOR</> loops, and
1135
+ dynamic record-set <command>FOR</>
1136
+ loops). <literal>FOUND</literal> is only set when the
1137
+ <command>FOR</> loop exits: inside the execution of the loop,
1138
+ <literal>FOUND</literal> is not modified by the
1139
+ <command>FOR</> statement, although it may be changed by the
1140
+ execution of other statements within the loop body.
1138
1141
</para>
1139
1142
</listitem>
1140
1143
</itemizedlist>
@@ -1975,7 +1978,7 @@ RAISE EXCEPTION ''Inexistent ID --> %'',user_id;
1975
1978
<application>PL/pgSQL</application> can be used to define trigger
1976
1979
procedures. A trigger procedure is created with the
1977
1980
<command>CREATE FUNCTION</> command as a function with no
1978
- arguments and a return type of <type>TRIGGER </type>. Note that
1981
+ arguments and a return type of <type>trigger </type>. Note that
1979
1982
the function must be declared with no arguments even if it expects
1980
1983
to receive arguments specified in <command>CREATE TRIGGER</> ---
1981
1984
trigger arguments are passed via <varname>TG_ARGV</>, as described
@@ -1992,8 +1995,9 @@ RAISE EXCEPTION ''Inexistent ID --> %'',user_id;
1992
1995
<term><varname>NEW</varname></term>
1993
1996
<listitem>
1994
1997
<para>
1995
- Data type <type>RECORD</type>; variable holding the new database row for INSERT/UPDATE
1996
- operations in ROW level triggers.
1998
+ Data type <type>RECORD</type>; variable holding the new
1999
+ database row for INSERT/UPDATE operations in ROW level
2000
+ triggers. This variable is NULL in STATEMENT level triggers.
1997
2001
</para>
1998
2002
</listitem>
1999
2003
</varlistentry>
@@ -2002,8 +2006,9 @@ RAISE EXCEPTION ''Inexistent ID --> %'',user_id;
2002
2006
<term><varname>OLD</varname></term>
2003
2007
<listitem>
2004
2008
<para>
2005
- Data type <type>RECORD</type>; variable holding the old database row for UPDATE/DELETE
2006
- operations in ROW level triggers.
2009
+ Data type <type>RECORD</type>; variable holding the old
2010
+ database row for UPDATE/DELETE operations in ROW level
2011
+ triggers. This variable is NULL in STATEMENT level triggers.
2007
2012
</para>
2008
2013
</listitem>
2009
2014
</varlistentry>
@@ -2098,22 +2103,23 @@ RAISE EXCEPTION ''Inexistent ID --> %'',user_id;
2098
2103
2099
2104
<para>
2100
2105
A trigger function must return either NULL or a record/row value
2101
- having exactly the structure of the table the trigger was fired for.
2102
- Triggers fired BEFORE may return NULL to signal the trigger manager
2103
- to skip the rest of the operation for this row (ie, subsequent triggers
2104
- are not fired, and the INSERT/UPDATE/DELETE does not occur for this
2105
- row). If a non-NULL value is returned then the operation proceeds with
2106
- that row value. Note that returning a row value different from the
2107
- original value of NEW alters the row that will be inserted or updated.
2108
- It is possible to replace single values directly
2109
- in NEW and return that, or to build a complete new record/row to
2110
- return.
2106
+ having exactly the structure of the table the trigger was fired
2107
+ for. The return value of a BEFORE or AFTER STATEMENT level
2108
+ trigger, or an AFTER ROW level trigger is ignored; it may as well
2109
+ return NULL. However, any of these types of triggers can still
2110
+ abort the entire trigger operation by raising an error.
2111
2111
</para>
2112
2112
2113
2113
<para>
2114
- The return value of a trigger fired AFTER is ignored; it may as well
2115
- always return a NULL value. But an AFTER trigger can still abort the
2116
- operation by raising an error.
2114
+ ROW level triggers fired BEFORE may return NULL to signal the
2115
+ trigger manager to skip the rest of the operation for this row
2116
+ (ie, subsequent triggers are not fired, and the
2117
+ INSERT/UPDATE/DELETE does not occur for this row). If a non-NULL
2118
+ value is returned then the operation proceeds with that row value.
2119
+ Note that returning a row value different from the original value
2120
+ of NEW alters the row that will be inserted or updated. It is
2121
+ possible to replace single values directly in NEW and return that,
2122
+ or to build a complete new record/row to return.
2117
2123
</para>
2118
2124
2119
2125
<example>
@@ -2143,7 +2149,7 @@ CREATE FUNCTION emp_stamp () RETURNS TRIGGER AS '
2143
2149
RAISE EXCEPTION ''% cannot have NULL salary'', NEW.empname;
2144
2150
END IF;
2145
2151
2146
- -- Who works for us when she must pay for?
2152
+ -- Who works for us when she must pay for it ?
2147
2153
IF NEW.salary < 0 THEN
2148
2154
RAISE EXCEPTION ''% cannot have a negative salary'', NEW.empname;
2149
2155
END IF;
0 commit comments