|
1 |
| -<!-- $PostgreSQL: pgsql/doc/src/sgml/plpgsql.sgml,v 1.116 2007/07/25 04:19:08 neilc Exp $ --> |
| 1 | +<!-- $PostgreSQL: pgsql/doc/src/sgml/plpgsql.sgml,v 1.117 2007/10/26 01:11:09 momjian Exp $ --> |
2 | 2 |
|
3 | 3 | <chapter id="plpgsql">
|
4 | 4 | <title><application>PL/pgSQL</application> - <acronym>SQL</acronym> Procedural Language</title>
|
@@ -1411,16 +1411,37 @@ RETURN QUERY <replaceable>query</replaceable>;
|
1411 | 1411 | </para>
|
1412 | 1412 |
|
1413 | 1413 | <para>
|
1414 |
| - Functions that use <command>RETURN NEXT</command> or |
1415 |
| - <command>RETURN QUERY</command> should be called in the |
1416 |
| - following fashion: |
| 1414 | + Here is an example of a function using <command>RETURN |
| 1415 | + NEXT</command>: |
1417 | 1416 |
|
1418 | 1417 | <programlisting>
|
1419 |
| -SELECT * FROM some_func(); |
| 1418 | +CREATE TABLE foo (fooid INT, foosubid INT, fooname TEXT); |
| 1419 | +INSERT INTO foo VALUES (1, 2, 'three'); |
| 1420 | +INSERT INTO foo VALUES (4, 5, 'six'); |
| 1421 | + |
| 1422 | +CREATE OR REPLACE FUNCTION getAllFoo() RETURNS SETOF foo AS |
| 1423 | +$BODY$ |
| 1424 | +DECLARE |
| 1425 | + r foo%rowtype; |
| 1426 | +BEGIN |
| 1427 | + FOR r IN SELECT * FROM foo |
| 1428 | + WHERE fooid > 0 |
| 1429 | + LOOP |
| 1430 | + -- can do some processing here |
| 1431 | + RETURN NEXT r; -- return next row of SELECT |
| 1432 | + END LOOP; |
| 1433 | + RETURN; |
| 1434 | +END |
| 1435 | +$BODY$ |
| 1436 | +LANGUAGE 'plpgsql' ; |
| 1437 | + |
| 1438 | +SELECT * FROM getallfoo(); |
1420 | 1439 | </programlisting>
|
1421 | 1440 |
|
1422 |
| - That is, the function must be used as a table source in a |
1423 |
| - <literal>FROM</literal> clause. |
| 1441 | + Note that functions using <command>RETURN NEXT</command> or |
| 1442 | + <command>RETURN QUERY</command> must be called as a table source in |
| 1443 | + a <literal>FROM</literal> clause. |
| 1444 | + |
1424 | 1445 | </para>
|
1425 | 1446 |
|
1426 | 1447 | <note>
|
|
0 commit comments