1
- <!-- $PostgreSQL: pgsql/doc/src/sgml/xfunc.sgml,v 1.131 2008/07/16 01:30:21 tgl Exp $ -->
1
+ <!-- $PostgreSQL: pgsql/doc/src/sgml/xfunc.sgml,v 1.132 2008/07/18 03:32:52 tgl Exp $ -->
2
2
3
3
<sect1 id="xfunc">
4
4
<title>User-Defined Functions</title>
94
94
</para>
95
95
96
96
<para>
97
- <indexterm><primary>SETOF</><seealso>function</></> Alternatively,
98
- an SQL function can be declared to return a set, by specifying the
99
- function's return type as <literal>SETOF
100
- <replaceable>sometype</></literal>. In this case all rows of the
101
- last query's result are returned. Further details appear below.
97
+ Alternatively, an SQL function can be declared to return a set,
98
+ by specifying the function's return type as <literal>SETOF
99
+ <replaceable>sometype</></literal>, or equivalently by declaring it as
100
+ <literal>RETURNS TABLE(<replaceable>columns</>)</literal>. In this case
101
+ all rows of the last query's result are returned. Further details appear
102
+ below.
102
103
</para>
103
104
104
105
<para>
117
118
other SQL commands. (The only exception is that you cannot put
118
119
<command>BEGIN</>, <command>COMMIT</>, <command>ROLLBACK</>, or
119
120
<command>SAVEPOINT</> commands into a <acronym>SQL</acronym> function.)
120
- However, the final command
121
+ However, the final command
121
122
must be a <command>SELECT</command> that returns whatever is
122
123
specified as the function's return type. Alternatively, if you
123
124
want to define a SQL function that performs actions but has no
@@ -175,7 +176,7 @@ INSERT INTO $1 VALUES (42);
175
176
<para>
176
177
The simplest possible <acronym>SQL</acronym> function has no arguments and
177
178
simply returns a base type, such as <type>integer</type>:
178
-
179
+
179
180
<screen>
180
181
CREATE FUNCTION one() RETURNS integer AS $$
181
182
SELECT 1 AS result;
@@ -202,7 +203,7 @@ SELECT one();
202
203
</para>
203
204
204
205
<para>
205
- It is almost as easy to define <acronym>SQL</acronym> functions
206
+ It is almost as easy to define <acronym>SQL</acronym> functions
206
207
that take base types as arguments. In the example below, notice
207
208
how we refer to the arguments within the function as <literal>$1</>
208
209
and <literal>$2</>.
@@ -226,7 +227,7 @@ SELECT add_em(1, 2) AS answer;
226
227
227
228
<programlisting>
228
229
CREATE FUNCTION tf1 (integer, numeric) RETURNS integer AS $$
229
- UPDATE bank
230
+ UPDATE bank
230
231
SET balance = balance - $2
231
232
WHERE accountno = $1;
232
233
SELECT 1;
@@ -248,7 +249,7 @@ SELECT tf1(17, 100.0);
248
249
249
250
<programlisting>
250
251
CREATE FUNCTION tf1 (integer, numeric) RETURNS numeric AS $$
251
- UPDATE bank
252
+ UPDATE bank
252
253
SET balance = balance - $2
253
254
WHERE accountno = $1;
254
255
SELECT balance FROM bank WHERE accountno = $1;
@@ -267,7 +268,7 @@ $$ LANGUAGE SQL;
267
268
types, we must not only specify which
268
269
argument we want (as we did above with <literal>$1</> and <literal>$2</literal>) but
269
270
also the desired attribute (field) of that argument. For example,
270
- suppose that
271
+ suppose that
271
272
<type>emp</type> is a table containing employee data, and therefore
272
273
also the name of the composite type of each row of the table. Here
273
274
is a function <function>double_salary</function> that computes what someone's
@@ -323,7 +324,7 @@ SELECT name, double_salary(ROW(name, salary*1.1, age, cubicle)) AS dream
323
324
324
325
<para>
325
326
It is also possible to build a function that returns a composite type.
326
- This is an example of a function
327
+ This is an example of a function
327
328
that returns a single <type>emp</type> row:
328
329
329
330
<programlisting>
@@ -364,7 +365,7 @@ ERROR: function declared to return emp returns varchar instead of text at colum
364
365
</para>
365
366
</listitem>
366
367
</itemizedlist>
367
- </para>
368
+ </para>
368
369
369
370
<para>
370
371
A different way to define the same function is:
@@ -380,7 +381,7 @@ $$ LANGUAGE SQL;
380
381
in this situation, but it is a handy alternative in some cases
381
382
— for example, if we need to compute the result by calling
382
383
another function that returns the desired composite value.
383
- </para>
384
+ </para>
384
385
385
386
<para>
386
387
We could call this function directly in either of two ways:
@@ -401,7 +402,7 @@ SELECT * FROM new_emp();
401
402
402
403
The second way is described more fully in <xref
403
404
linkend="xfunc-sql-table-functions">.
404
- </para>
405
+ </para>
405
406
406
407
<para>
407
408
When you use a function that returns a composite type,
@@ -429,7 +430,7 @@ LINE 1: SELECT new_emp().name;
429
430
430
431
<para>
431
432
Another option is to use
432
- functional notation for extracting an attribute. The simple way
433
+ functional notation for extracting an attribute. The simple way
433
434
to explain this is that we can use the
434
435
notations <literal>attribute(table)</> and <literal>table.attribute</>
435
436
interchangeably.
@@ -693,9 +694,14 @@ SELECT *, upper(fooname) FROM getfoo(1) AS t1;
693
694
</para>
694
695
</sect2>
695
696
696
- <sect2>
697
+ <sect2 id="xfunc-sql-functions-returning-set" >
697
698
<title><acronym>SQL</acronym> Functions Returning Sets</title>
698
699
700
+ <indexterm>
701
+ <primary>function</primary>
702
+ <secondary>with SETOF</secondary>
703
+ </indexterm>
704
+
699
705
<para>
700
706
When an SQL function is declared as returning <literal>SETOF
701
707
<replaceable>sometype</></literal>, the function's final
@@ -733,7 +739,7 @@ SELECT * FROM getfoo(1) AS t1;
733
739
734
740
<programlisting>
735
741
CREATE FUNCTION sum_n_product_with_tab (x int, OUT sum int, OUT product int) RETURNS SETOF record AS $$
736
- SELECT x + tab.y, x * tab.y FROM tab;
742
+ SELECT $1 + tab.y, $1 * tab.y FROM tab;
737
743
$$ LANGUAGE SQL;
738
744
</programlisting>
739
745
@@ -794,6 +800,41 @@ SELECT name, listchildren(name) FROM nodes;
794
800
</para>
795
801
</sect2>
796
802
803
+ <sect2 id="xfunc-sql-functions-returning-table">
804
+ <title><acronym>SQL</acronym> Functions Returning <literal>TABLE</></title>
805
+
806
+ <indexterm>
807
+ <primary>function</primary>
808
+ <secondary>RETURNS TABLE</secondary>
809
+ </indexterm>
810
+
811
+ <para>
812
+ There is another way to declare a function as returning a set,
813
+ which is to use the syntax
814
+ <literal>RETURNS TABLE(<replaceable>columns</>)</literal>.
815
+ This is equivalent to using one or more <literal>OUT</> parameters plus
816
+ marking the function as returning <literal>SETOF record</> (or
817
+ <literal>SETOF</> a single output parameter's type, as appropriate).
818
+ This notation is specified in recent versions of the SQL standard, and
819
+ thus may be more portable than using <literal>SETOF</>.
820
+ </para>
821
+
822
+ <para>
823
+ For example, the preceding sum-and-product example could also be
824
+ done this way:
825
+
826
+ <programlisting>
827
+ CREATE FUNCTION sum_n_product_with_tab (x int) RETURNS TABLE(sum int, product int) AS $$
828
+ SELECT $1 + tab.y, $1 * tab.y FROM tab;
829
+ $$ LANGUAGE SQL;
830
+ </programlisting>
831
+
832
+ It is not allowed to use explicit <literal>OUT</> or <literal>INOUT</>
833
+ parameters with the <literal>RETURNS TABLE</> notation — you must
834
+ put all the output columns in the <literal>TABLE</> list.
835
+ </para>
836
+ </sect2>
837
+
797
838
<sect2>
798
839
<title>Polymorphic <acronym>SQL</acronym> Functions</title>
799
840
0 commit comments