1
1
<!--
2
- $Header: /cvsroot/pgsql/doc/src/sgml/Attic/plsql.sgml,v 2.54 2002/03/22 19:20:18 petere Exp $
2
+ $Header: /cvsroot/pgsql/doc/src/sgml/Attic/plsql.sgml,v 2.55 2002/04/09 02:31:58 momjian Exp $
3
3
-->
4
4
5
5
<chapter id="plpgsql">
@@ -762,7 +762,7 @@ CREATE FUNCTION logfunc2 (TEXT) RETURNS TIMESTAMP AS '
762
762
<para>
763
763
If the expression's result data type doesn't match the variable's
764
764
data type, or the variable has a specific size/precision
765
- (as for <type>char(20)</type>), the result value will be implicitly
765
+ (like <type>char(20)</type>), the result value will be implicitly
766
766
converted by the <application>PL/pgSQL</application> interpreter using
767
767
the result type's output-function and
768
768
the variable type's input-function. Note that this could potentially
@@ -880,7 +880,7 @@ PERFORM <replaceable>query</replaceable>;
880
880
This executes a <literal>SELECT</literal>
881
881
<replaceable>query</replaceable> and discards the
882
882
result. <application>PL/pgSQL</application> variables are substituted
883
- into the query as usual.
883
+ in the query as usual.
884
884
</para>
885
885
886
886
<note>
@@ -927,7 +927,7 @@ EXECUTE <replaceable class="command">query-string</replaceable>;
927
927
<para>
928
928
Note in particular that no substitution of <application>PL/pgSQL</>
929
929
variables is done on the query string. The values of variables must
930
- be inserted into the query string as it is constructed.
930
+ be inserted in the query string as it is constructed.
931
931
</para>
932
932
933
933
<para>
@@ -1441,16 +1441,16 @@ END LOOP;
1441
1441
<title>Cursors</title>
1442
1442
1443
1443
<para>
1444
- Rather than executing a whole query at once, it is possible to
1445
- set up a <firstterm>cursor</> that encapsulates the query, and
1446
- then read the query result a few rows at a time. One reason
1447
- for doing this is to avoid memory overrun when the result contains
1448
- a large number of rows. (However, <application>PL/pgSQL</> users
1449
- don't normally need to worry about that, since FOR loops automatically
1450
- use a cursor internally to avoid memory problems.) A more interesting
1451
- possibility is that a function can return a reference to a cursor
1452
- that it has set up, allowing the caller to read the rows. This
1453
- provides one way of returning a row set from a function.
1444
+ Rather than executing a whole query at once, it is possible to set
1445
+ up a <firstterm>cursor</> that encapsulates the query, and then read
1446
+ the query result a few rows at a time. One reason for doing this is
1447
+ to avoid memory overrun when the result contains a large number of
1448
+ rows. (However, <application>PL/pgSQL</> users don't normally need
1449
+ to worry about that, since FOR loops automatically use a cursor
1450
+ internally to avoid memory problems.) A more interesting usage is to
1451
+ return a reference to a cursor that it has created, allowing the
1452
+ caller to read the rows. This provides one way of returning multiple
1453
+ rows and columns from a function.
1454
1454
</para>
1455
1455
1456
1456
<sect2 id="plpgsql-cursor-declarations">
@@ -1498,11 +1498,10 @@ DECLARE
1498
1498
1499
1499
<para>
1500
1500
Before a cursor can be used to retrieve rows, it must be
1501
- <firstterm>opened</>. (This is the equivalent action to
1502
- the SQL command <command>DECLARE CURSOR</>.)
1503
- <application>PL/pgSQL</> has four forms of the OPEN statement,
1504
- two of which are for use with unbound cursor variables
1505
- and the other two for use with bound cursor variables.
1501
+ <firstterm>opened</>. (This is the equivalent action to the SQL
1502
+ command <command>DECLARE CURSOR</>.) <application>PL/pgSQL</> has
1503
+ four forms of the OPEN statement, two of which use unbound cursor
1504
+ variables and the other two use bound cursor variables.
1506
1505
</para>
1507
1506
1508
1507
<sect3>
@@ -1518,7 +1517,7 @@ OPEN <replaceable>unbound-cursor</replaceable> FOR SELECT ...;
1518
1517
have been declared as an unbound cursor (that is, as a simple
1519
1518
<type>refcursor</> variable). The SELECT query is treated
1520
1519
in the same way as other SELECTs in <application>PL/pgSQL</>:
1521
- <application>PL/pgSQL</> variable names are substituted for ,
1520
+ <application>PL/pgSQL</> variable names are substituted,
1522
1521
and the query plan is cached for possible re-use.
1523
1522
1524
1523
<programlisting>
@@ -1539,8 +1538,8 @@ OPEN <replaceable>unbound-cursor</replaceable> FOR EXECUTE <replaceable class="c
1539
1538
to execute. The cursor cannot be open already, and it must
1540
1539
have been declared as an unbound cursor (that is, as a simple
1541
1540
<type>refcursor</> variable). The query is specified as a
1542
- string expression in the same way as for the EXECUTE command.
1543
- As usual, this gives flexibility for the query to vary
1541
+ string expression in the same way as in the EXECUTE command.
1542
+ As usual, this gives flexibility so the query can vary
1544
1543
from one run to the next.
1545
1544
1546
1545
<programlisting>
@@ -1562,7 +1561,7 @@ OPEN <replaceable>bound-cursor</replaceable> <optional> ( <replaceable>argument_
1562
1561
The cursor cannot be open already. A list of actual argument
1563
1562
value expressions must appear if and only if the cursor was
1564
1563
declared to take arguments. These values will be substituted
1565
- into the query.
1564
+ in the query.
1566
1565
The query plan for a bound cursor is always considered
1567
1566
cacheable --- there is no equivalent of EXECUTE in this case.
1568
1567
@@ -1593,7 +1592,7 @@ OPEN curs3(42);
1593
1592
</para>
1594
1593
1595
1594
<para>
1596
- All Portals are implicitly closed at end of transaction. Therefore
1595
+ All Portals are implicitly closed at transaction end . Therefore
1597
1596
a <type>refcursor</> value is useful to reference an open cursor
1598
1597
only until the end of the transaction.
1599
1598
</para>
@@ -1608,7 +1607,7 @@ FETCH <replaceable>cursor</replaceable> INTO <replaceable>target</replaceable>;
1608
1607
1609
1608
FETCH retrieves the next row from the cursor into a target,
1610
1609
which may be a row variable, a record variable, or a comma-separated
1611
- list of simple variables, just as for SELECT INTO. As with
1610
+ list of simple variables, just like SELECT INTO. As with
1612
1611
SELECT INTO, the special variable FOUND may be checked to see
1613
1612
whether a row was obtained or not.
1614
1613
@@ -1633,6 +1632,70 @@ CLOSE <replaceable>cursor</replaceable>;
1633
1632
1634
1633
<programlisting>
1635
1634
CLOSE curs1;
1635
+ </programlisting>
1636
+ </para>
1637
+ </sect3>
1638
+
1639
+ <sect3>
1640
+ <title>Returning Cursors</title>
1641
+
1642
+ <para>
1643
+
1644
+ <application>PL/pgSQL</> functions can return cursors to the
1645
+ caller. This is used to return multiple rows or columns from the
1646
+ function. The function opens the cursor and returns the cursor
1647
+ name to the caller. The caller can then FETCH rows from the
1648
+ cursor. The cursor can be CLOSEd by the caller, or it will be
1649
+ closed automatically when the transaction closes.
1650
+
1651
+ </para>
1652
+
1653
+ <para>
1654
+ The cursor name returned by the function can be specified by the
1655
+ caller or automatically generated. The following example shows
1656
+ how a cursor name can be supplied by the caller:
1657
+
1658
+ <programlisting>
1659
+ CREATE TABLE test (col text);
1660
+ INSERT INTO test VALUES ('123');
1661
+
1662
+ CREATE FUNCTION reffunc(refcursor) RETURNS refcursor AS '
1663
+ BEGIN
1664
+ OPEN $1 FOR SELECT col FROM test;
1665
+ RETURN $1;
1666
+ END;
1667
+ ' LANGUAGE 'plpgsql';
1668
+
1669
+ BEGIN;
1670
+ SELECT reffunc('funccursor');
1671
+ FETCH ALL IN funccursor;
1672
+ COMMIT;
1673
+ </programlisting>
1674
+ </para>
1675
+
1676
+ <para>
1677
+ The following example uses automatic cursor name generation:
1678
+
1679
+ <programlisting>
1680
+ CREATE FUNCTION reffunc2() RETURNS refcursor AS '
1681
+ DECLARE
1682
+ ref refcursor;
1683
+ BEGIN
1684
+ OPEN ref FOR SELECT col FROM test;
1685
+ RETURN ref;
1686
+ END;
1687
+ ' LANGUAGE 'plpgsql';
1688
+
1689
+ BEGIN;
1690
+ SELECT reffunc2();
1691
+
1692
+ reffunc2
1693
+ --------------------
1694
+ <unnamed cursor 1>
1695
+ (1 row)
1696
+
1697
+ FETCH ALL IN "<unnamed cursor 1>";
1698
+ COMMIT;
1636
1699
</programlisting>
1637
1700
</para>
1638
1701
</sect3>
0 commit comments