pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Richard Huxton <dev@archonet.com>
To: Andrew Hall <andrewah@hotmail.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: Table Valued Parameters
Date: Sat, 24 Oct 2009 13:01:07 +0100
Message-ID: <4AE2EC83.6060908@archonet.com> (raw)
In-Reply-To: <COL122-W14509AEC755F5710A3CA19CDBD0@phx.gbl>
References: <COL122-W14509AEC755F5710A3CA19CDBD0@phx.gbl>

Andrew Hall wrote:
> Hi,
> 
> I was wondering whether anybody would be able to advise me on how (if it is possible) to port some functionality from Oracle?
> 
> This is just an example - in Oracle, I am able to do the following 
> 
> --
> -- Create a data type which replicates the data structure of a single user in my application.
> -- I know that this can be done using PostgreSQL.

> -- Create a data type which can store many instances of a single 'TY_APP_USER' 
> -- [essentially this is a table valued data type]. An instance of this data type can be
> -- created and populated by the client application [a java based one in my case].
> --
> -- I can't find any reference to something 
> -- similar to this using postgreSQL.

The following may not do anything interesting, but it does show arrays
of composite types, which is what you are after.

To prevent quoting insanity, I recommend the ARRAY[] constructor rather
than array literals. You do need the explicit typecasts.

Oh - and version 8.3 or higher for arrays of compound types.


BEGIN;

CREATE TYPE typ1 AS (i integer, t text);

CREATE FUNCTION print_array(a typ1[]) RETURNS void AS $$
DECLARE
    m int;
    n int;
    i int;
    e typ1;
BEGIN
    m := array_lower(a, 1);
    n := array_upper(a, 1);
    FOR i IN m .. n LOOP
        e := a[i];
        RAISE NOTICE '% - %', e.i, e.t;
    END LOOP;
    RETURN;
END;
$$ LANGUAGE plpgsql;

SELECT print_array(ARRAY[ '(1,"abc")'::typ1, '(2,"def")'::typ1 ]);
SELECT print_array(ARRAY[ '(1,"abc")', '(2,"def")' ]::typ1[]);

ROLLBACK;

-- 
  Richard Huxton
  Archonet Ltd



view thread (8+ messages)  latest in thread

Message-ID: <4AE2EC83.6060908@archonet.com>
Permalink:  ../4AE2EC83.6060908@archonet.com/
Also on:    postgresql.org/message-id/4AE2EC83.6060908@archonet.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: dev@archonet.com, andrewah@hotmail.com
  Subject: Re: Table Valued Parameters
  In-Reply-To: <4AE2EC83.6060908@archonet.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