Add plpgsql doc example of RETURN NEXT.
authorBruce Momjian
Fri, 26 Oct 2007 01:11:09 +0000 (01:11 +0000)
committerBruce Momjian
Fri, 26 Oct 2007 01:11:09 +0000 (01:11 +0000)
Ulrich Kroener

doc/src/sgml/plpgsql.sgml

index 70a0c4e6dcca670442031aaa7570aeacb4b68dcc..4cbb9c4df5286361c6923c77eba2c0cae5532d2d 100644 (file)
@@ -1,4 +1,4 @@
-
+
 
  
   <application>PL/pgSQL</application> - <acronym>SQL</acronym> Procedural Language
@@ -1411,16 +1411,37 @@ RETURN QUERY query;
      
 
      
-      Functions that use RETURN NEXT or
-      RETURN QUERY should be called in the
-      following fashion:
+      Here is an example of a function using RETURN
+      NEXT:
 
 
-SELECT * FROM some_func();
+CREATE TABLE foo (fooid INT, foosubid INT, fooname TEXT);
+INSERT INTO foo VALUES (1, 2, 'three');
+INSERT INTO foo VALUES (4, 5, 'six');
+
+CREATE OR REPLACE FUNCTION getAllFoo() RETURNS SETOF foo AS
+$BODY$
+DECLARE
+    r foo%rowtype;
+BEGIN
+    FOR r IN SELECT * FROM foo
+    WHERE fooid > 0
+    LOOP
+        -- can do some processing here
+        RETURN NEXT r; -- return next row of SELECT
+    END LOOP;
+    RETURN;
+END
+$BODY$
+LANGUAGE 'plpgsql' ;
+
+SELECT * FROM getallfoo();
 
 
-      That is, the function must be used as a table source in a
-      FROM clause.
+      Note that functions using RETURN NEXT or
+      RETURN QUERY must be called as a table source in
+      a FROM clause.
+