Re: PostgreSQL equivalent to Oracles ANYDATASET

From: Christoph Moench-Tegeder <cmt(at)burggraben(dot)net>
To: Dirk Mika <Dirk(dot)Mika(at)mikatiming(dot)de>
Cc: "pgsql-general(at)lists(dot)postgresql(dot)org" <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: PostgreSQL equivalent to Oracles ANYDATASET
Date: 2020-11-13 17:23:01
Message-ID: X67A9V2P8HkC/AF8@elch.exwg.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

## Dirk Mika (Dirk(dot)Mika(at)mikatiming(dot)de):

> SELECT * FROM TABLE(series_pkg.get_results(1));
>
> The purpose of this function is to provide a DATASET, which has
> different columns in the result depending on the passed parameter.
>
> Is there any way to achieve something similar in PostreSQL?

testing=# CREATE OR REPLACE FUNCTION public.rr(p INTEGER)
RETURNS SETOF RECORD
LANGUAGE plpgsql
AS $function$
BEGIN
IF p = 1 THEN
RETURN NEXT ('k1'::TEXT, 'v1'::TEXT);
RETURN NEXT ('k2'::TEXT, 'v2'::TEXT);
ELSE
RETURN NEXT (23::INTEGER, 42::INTEGER, 'abc'::TEXT);
RETURN NEXT (42::INTEGER, 23::INTEGER, 'xyz'::TEXT);
END IF;
RETURN;
END;
$function$;
CREATE FUNCTION

testing=# SELECT * FROM rr(2) f(a INTEGER, b INTEGER, c TEXT);
a | b | c
----+----+-----
23 | 42 | abc
42 | 23 | xyz
(2 rows)

testing=# SELECT * FROM rr(1) f(x TEXT, y TEXT);
x | y
----+----
k1 | v1
k2 | v2
(2 rows)

Regards,
Christoph

--
Spare Space

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Laurenz Albe 2020-11-13 17:37:36 Re: conflict with recovery when delay is gone
Previous Message Jeremy Wilson 2020-11-13 17:12:56 Re: Issue upgrading from 9.5 to 13 with pg_upgrade: "connection to database failed: FATAL: database "template1" does not exist"