pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Pavel Stehule <pavel.stehule@gmail.com>
To: Michele Petrazzo - Unipex srl <michele.petrazzo@unipex.it>
Cc: Pgsql-Sql <pgsql-sql@postgresql.org>
Subject: Re: functions are returns columns
Date: Fri, 9 Nov 2007 21:35:20 +0100
Message-ID: <162867790711091235i7e77abcch27731d91285949b6@mail.gmail.com> (raw)
In-Reply-To: <4734B921.5050704@unipex.it>
References: <4734B921.5050704@unipex.it>

On 09/11/2007, Michele Petrazzo - Unipex srl <michele.petrazzo@unipex.it> wrote:
> Hi all.
> I want that a function return a table rows (like the doc says at 33.4.4.
> SQL Functions as Table Sources), but I want the a function return only a
> few cols, so the same that I select into the func.
> Modifying the doc example:
>
> CREATE TABLE foo (fooid int, foosubid int, fooname text);
> INSERT INTO foo VALUES (1, 1, 'Joe');
> INSERT INTO foo VALUES (1, 2, 'Ed');
> INSERT INTO foo VALUES (2, 1, 'Mary');
>
> CREATE FUNCTION getfoo(int) RETURNS foo AS $$
>      SELECT fooid, foosubid FROM foo WHERE fooid = $1;
> $$ LANGUAGE SQL;
>
> This give me an error:
>
> ERROR:  return type mismatch in function declared to return foo
> DETAIL:  Final SELECT returns too few columns.
> CONTEXT:  SQL function "getfoo"
>
>

CREATE FUNCTION getfoo(int) RETURNS foo AS $$
      SELECT fooid, foosubid FROM foo WHERE fooid = $1 LIMIT 1;
$$ LANGUAGE SQL;

or

 CREATE FUNCTION getfoo(int) RETURNS SETOF foo AS $$
      SELECT fooid, foosubid FROM foo WHERE fooid = $1;
 $$ LANGUAGE SQL;

try:
SELECT * FROM getfoo(1);

Regards

Pavel Stehule



> So, how do it?
>
> Thanks,
> Michele
>
> ---------------------------(end of broadcast)---------------------------
> TIP 5: don't forget to increase your free space map settings
>



view thread (7+ messages)  latest in thread

Message-ID: <162867790711091235i7e77abcch27731d91285949b6@mail.gmail.com>
Permalink:  ../162867790711091235i7e77abcch27731d91285949b6@mail.gmail.com/
Also on:    postgresql.org/message-id/162867790711091235i7e77abcch27731d91285949b6@mail.gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: pavel.stehule@gmail.com, michele.petrazzo@unipex.it
  Subject: Re: functions are returns columns
  In-Reply-To: <162867790711091235i7e77abcch27731d91285949b6@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox