pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Bryan Klimt <Bryan_Klimt@baylor.edu>
To: pgsql-sql@postgresql.org
Subject: Re: insert rowtype
Date: Thu, 8 May 2003 11:41:04 -0500
Message-ID: <DBDAE5D0-8173-11D7-B60D-000A95774E48@baylor.edu> (raw)
In-Reply-To: <22B5FF60-815D-11D7-B60D-000A95774E48@baylor.edu>


So, to answer my own question...

I wanted to duplicate a row, but change a few data items in it.
Here is an example table to demonstrate:

# select oid,* from test;

   oid  | id |       tmstamp       | description
-------+----+---------------------+-------------
  20396 |  1 | 0000-00-00-00-00-00 | hello
  20397 |  2 | 0000-00-00-00-00-00 | goodbye
  20398 |  2 | 0000-00-00-00-00-01 | goodbye

I wanted to create a new row like this:

   oid  | id |       tmstamp       | description
-------+----+---------------------+-------------
  20396 |  1 | 0000-00-00-00-00-00 | hello
  20397 |  2 | 0000-00-00-00-00-00 | goodbye
  20398 |  2 | 0000-00-00-00-00-01 | goodbye
  20415 |  2 | 1111-00-00-00-00-02 | goodbye

But with my real table there are like 50 columns, and i didn't want to 
retype all their names.
So, I created this function:

create or replace function updatetest(integer,varchar(32)) returns 
integer as
	'
	declare
		-- the oid of the new row
		newoid integer;
	begin
		-- duplicate the row
		insert into test
			select * from test t where
				t.tmstamp=(
					select max(tt.tmstamp) from test tt where tt.id=t.id
				)
				and t.id=$1;

		-- get the oid of the new duplicate
		get diagnostics newoid = result_oid;

		-- update the columns you want to change
		update test set tmstamp=$2 where oid=newoid;

		-- return the new oid
		return newoid;
	end;
	' language plpgsql;

# select updatetest(2,'1111-00-00-00-00-02');

  updatetest
------------
       20415

I know it looks kinda obvious but it took me forever to figure it out.
Specifically, "get diagnostics newoid = result_oid;" is not very 
obvious to plpgsql newbies.

-Bryan




view thread (4+ messages)  latest in thread

Message-ID: <DBDAE5D0-8173-11D7-B60D-000A95774E48@baylor.edu>
Permalink:  ../DBDAE5D0-8173-11D7-B60D-000A95774E48@baylor.edu/
Also on:    postgresql.org/message-id/DBDAE5D0-8173-11D7-B60D-000A95774E48@baylor.edu

 · 

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-sql@postgresql.org
  Cc: Bryan_Klimt@baylor.edu
  Subject: Re: insert rowtype
  In-Reply-To: <DBDAE5D0-8173-11D7-B60D-000A95774E48@baylor.edu>
  References: <22B5FF60-815D-11D7-B60D-000A95774E48@baylor.edu> <DBDAE5D0-8173-11D7-B60D-000A95774E48@baylor.edu>

* 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