pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Mario Splivalo <mario.splivalo@mobart.hr>
To: pgsql-sql@postgresql.org
Subject: INSERTing values from usertypes how-to?
Date: Thu, 27 Apr 2006 17:43:32 +0200
Message-ID: <1146152613.19362.11.camel@localhost.localdomain> (raw)

Comming back after SELECTing into usertypes, is there a nice way to
insert data into table from usertype, in a nice way?

For instance, I have table like this:

CREATE TABLE tblA (
	id int4,
	key varchar,
	value varchar
)

Then, in my function, I do this:

CREATE FUNCTION testInsert()
RETURNS void
AS
$BODY$
DECLARE
	insert_tblA tblA;

	insert_tblA.id = get_next_id_or_something();
	insert_tblA.key = get_user_key_or_something();
	insert_tblA.value = get_some_value();

	INSERT INTO tblA (
		id,
		key,
		value)
	VALUES (
		insert_tblA.id,
		insert_tblA.key,
		insert_tblA.value
	);

END
$BODY$ LANGUAGE 'plpgsql';


Now, in this particular example it seems stupid first to populate
usertype and then insert the data from it to the tblA table, but in my
actuall function that makes sense (the function is quite large for
posting here). Now, since sometimes I have 30 columns in a table, I
tried to find a way to simply 'pour' the usertype into the table. I
tried something like:

	INSERT INTO tblA (insert_tblaA);

but postgres complains (and I guess I expeted that) with an error saying
that tblA.id is of type int4, and I'm trying to insert value of type
tblA. So, is there a way to map all those type-members in a way so that
one could easily INSERT those into table?

	Mario




view thread (2+ messages)  latest in thread

Message-ID: <1146152613.19362.11.camel@localhost.localdomain>
Permalink:  ../1146152613.19362.11.camel@localhost.localdomain/
Also on:    postgresql.org/message-id/1146152613.19362.11.camel@localhost.localdomain

 · 

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: mario.splivalo@mobart.hr
  Subject: Re: INSERTing values from usertypes how-to?
  In-Reply-To: <1146152613.19362.11.camel@localhost.localdomain>

* 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