From 4d67961110d17768021bac2c00fd395942d03170 Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Tue, 23 Apr 2013 22:46:36 -0400 Subject: [PATCH] PL/pgSQL doc: Add example for RETURN QUERY MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Erwin Brandstetter and Pavel Stěhule --- doc/src/sgml/plpgsql.sgml | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/doc/src/sgml/plpgsql.sgml b/doc/src/sgml/plpgsql.sgml index 3b2b49d09b..dbea3cd280 100644 --- a/doc/src/sgml/plpgsql.sgml +++ b/doc/src/sgml/plpgsql.sgml @@ -1714,6 +1714,36 @@ SELECT * FROM get_all_foo(); + + Here is an example of a function using RETURN + QUERY: + + +CREATE FUNCTION get_available_flightid(date) RETURNS SETOF integer AS +$BODY$ +BEGIN + RETURN QUERY SELECT flightid + FROM flight + WHERE flightdate >= $1 + AND flightdate < ($1 + 1); + + -- Since execution is not finished, we can check whether rows were returned + -- and raise exception if not. + IF NOT FOUND THEN + RAISE EXCEPTION 'No flight at %.', $1; + END IF; + + RETURN; + END +$BODY$ +LANGUAGE plpgsql; + +-- Returns available flights or raises exception if there are no +-- available flights. +SELECT * FROM get_available_flightid(CURRENT_DATE); + + + The current implementation of RETURN NEXT