pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Ron <ronljohnsonjr@gmail.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: Ad hoc SETOF type definition?
Date: Tue, 26 Sep 2023 19:25:36 -0500
Message-ID: <72d8f382-90d9-bfd4-1b80-4fbe37770bd3@gmail.com> (raw)
In-Reply-To: <623fe0c1-bdb4-49f7-9115-284159b3b137@aklaver.com>
References: <9707e21e-eed3-3c06-821e-e935eab4c098@gmail.com>
	<432138.1695750386@sss.pgh.pa.us>
	<73c7a357-62fe-3274-7f27-7ef9adc3b9b5@gmail.com>
	<436021.1695752103@sss.pgh.pa.us>
	<f498d4d3-aaf6-3723-8bc9-8048d19fa302@gmail.com>
	<538ed08d-55ba-4a51-8e46-4e424aaebb3c@aklaver.com>
	<623fe0c1-bdb4-49f7-9115-284159b3b137@aklaver.com>

On 9/26/23 16:29, Adrian Klaver wrote:
[snip]
> As a very simple example:

This is EXACTLY what I was looking for.  Thank you.

>
> create table source(id integer, fld_1 varchar);
>
> insert into source values (1, 'cat'), (2, 'dog'), (3, 'fish');
>
> CREATE OR REPLACE FUNCTION public.table_return(multiplier integer, suffix 
> character varying)
>  RETURNS TABLE(multiplied integer, fld_suffix character varying, 
> rand_number numeric)
>  LANGUAGE plpgsql
> AS $function$
> DECLARE
>     _id    integer;
>     _fld   varchar;
> BEGIN
>
> FOR _id, _fld IN
>     SELECT
>         id, fld_1
>     FROM
>         source
>     LOOP
>         multiplied = _id * multiplier;
>         fld_suffix = _fld || '_' || suffix;
>         rand_number = random() * 100;
>
>         RETURN NEXT;
>     END LOOP;
>
> END;
> $function$
> ;
>
> select * from table_return(2, 'test');
>  multiplied | fld_suffix |   rand_number
> ------------+------------+------------------
>           2 | cat_test   | 79.7745033326483
>           4 | dog_test   | 12.5713231966519
>           6 | fish_test  | 3.21770069680842

-- 
Born in Arizona, moved to Babylonia.





view thread (10+ messages)  latest in thread

Message-ID: <72d8f382-90d9-bfd4-1b80-4fbe37770bd3@gmail.com>
Permalink:  ../72d8f382-90d9-bfd4-1b80-4fbe37770bd3@gmail.com/
Also on:    postgresql.org/message-id/72d8f382-90d9-bfd4-1b80-4fbe37770bd3@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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: ronljohnsonjr@gmail.com
  Subject: Re: Ad hoc SETOF type definition?
  In-Reply-To: <72d8f382-90d9-bfd4-1b80-4fbe37770bd3@gmail.com>
  References: <9707e21e-eed3-3c06-821e-e935eab4c098@gmail.com> <432138.1695750386@sss.pgh.pa.us> <73c7a357-62fe-3274-7f27-7ef9adc3b9b5@gmail.com> <436021.1695752103@sss.pgh.pa.us> <f498d4d3-aaf6-3723-8bc9-8048d19fa302@gmail.com> <538ed08d-55ba-4a51-8e46-4e424aaebb3c@aklaver.com> <623fe0c1-bdb4-49f7-9115-284159b3b137@aklaver.com> <72d8f382-90d9-bfd4-1b80-4fbe37770bd3@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