Code examples for 39.6.1. Returning From a Function - Mailing list pgsql-docs

From Erwin Brandstetter
Subject Code examples for 39.6.1. Returning From a Function
Date
Msg-id 5161EE50.6090004@falter.at
Whole thread Raw
Responses Re: Code examples for 39.6.1. Returning From a Function
List pgsql-docs
Aloha!

Repost, since the first attempt on 03.04.2013 14:31 was before I subsrcibed to pgsql-docs and doesn't seem to have arrived.

As advised by Pavel here:
http://stackoverflow.com/questions/15731247/postgresql-function-syntax-error/15731425#15731425
I am sending this as possible code example for:
http://www.postgresql.org/docs/current/interactive/plpgsql-control-structures.html#PLPGSQL-STATEMENTS-RETURNING

CREATE FUNCTION check_available_flightid(date) RETURNS SETOF integer AS 
$func$
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  IF NOT FOUND     RAISE EXCEPTION 'No flight at %.', $1;  END IF;
  RETURN;   
END
$func$ LANGUAGE plpgsql

While being at it, the example at
http://www.postgresql.org/docs/current/interactive/plpgsql-control-structures.html#AEN56293
could be improved.
Instead of:

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 current row of SELECT   END LOOP;   RETURN;
END
$BODY$
LANGUAGE 'plpgsql' ;
Use:
CREATE OR REPLACE FUNCTION get_all_foo() 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 current row of SELECT   END LOOP;   RETURN;
END
$BODY$
LANGUAGE plpgsql;

- Most importantly plpgsql is an identifier and should not be quoted!
- Using unquoted, mixed-case identifers (getAllFoo) is a bad example.
- Minor formatting.


Regards
Erwin


pgsql-docs by date:

Previous
From: 正品折扣护肤
Date:
Subject: 69元!抢购雅诗兰黛2011全能细胞亮白防晒调色BB霜SPF35/PA+++30ml隐没肌肤一切瑕疵,使肌肤均匀有致,更有不同凡响的防晒效果!让您出行更方便!(AD)
Next
From: Peter Eisentraut
Date:
Subject: DTrace home page down