Re: Returning multiple columns with a function??

From: Joe Conway <mail(at)joeconway(dot)com>
To: "Joshua D(dot) Drake" <jd(at)commandprompt(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Returning multiple columns with a function??
Date: 2002-12-17 00:48:53
Message-ID: 3DFE7475.7060508@joeconway.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Joshua D. Drake wrote:
> But something like this fails:
>
> CREATE OR REPLACE FUNCTION test_multiple () RETURNS SETOF text AS
> 'SELECT ''a'', ''b''' LANGUAGE 'SQL';
> ERROR: function declared to return text returns multiple columns in
> final SELECT
>
> What are we missing?

Try:
CREATE OR REPLACE FUNCTION test_1 () RETURNS SETOF record AS 'SELECT
''a''::text, ''b''::text' LANGUAGE 'SQL';

regression=# SELECT * FROM test_1() AS t(f1 text, f2 text);
f1 | f2
----+----
a | b
(1 row)

or:

CREATE TYPE mytype AS (f1 int, f2 text);
CREATE OR REPLACE FUNCTION test_2 () RETURNS SETOF mytype AS 'SELECT 1::int,
''b''::text' LANGUAGE 'SQL';

regression=# SELECT * FROM test_2();
f1 | f2
----+----
1 | b
(1 row)

See the info scattered amongst:

http://www.us.postgresql.org/users-lounge/docs/7.3/postgres/sql-select.html
http://www.us.postgresql.org/users-lounge/docs/7.3/postgres/sql-createtype.html
http://www.us.postgresql.org/users-lounge/docs/7.3/postgres/xfunc-tablefunctions.html
http://www.us.postgresql.org/users-lounge/docs/7.3/postgres/xfunc-sql.html
http://www.us.postgresql.org/users-lounge/docs/7.3/postgres/xfunc-c.html
http://www.us.postgresql.org/users-lounge/docs/7.3/postgres/plpgsql-control-structures.html

(and maybe some others)

HTH,

Joe

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Magnus Naeslund(f) 2002-12-17 01:00:47 Re: [HACKERS] following instructions GCC
Previous Message Hadley Willan 2002-12-17 00:17:40 Re: Changing a column's type